<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="theme-color" content="#0078E7"><meta name="author" content="zzl"><meta name="copyright" content="zzl"><meta name="generator" content="Hexo 5.4.0"><meta name="theme" content="hexo-theme-yun"><title>java中级 | zzl</title><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@900&amp;display=swap" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/star-markdown-css@0.1.25/dist/yun/yun-markdown.min.css"><script src="//at.alicdn.com/t/font_1140697_dxory92pb0h.js" async></script><script src="https://cdn.jsdelivr.net/npm/scrollreveal/dist/scrollreveal.min.js" defer></script><script>document.addEventListener("DOMContentLoaded", () => {
  [".post-card",".post-content img"].forEach((target)=> {
    ScrollReveal().reveal(target);
  })
});
</script><link rel="icon" href="/web/yun.svg"><link rel="mask-icon" href="/web/yun.svg" color="#0078E7"><link rel="alternate icon" href="/web/yun.ico"><link rel="preload" href="/web/css/hexo-theme-yun.css" as="style"><link rel="preload" href="/web/js/utils.js" as="script"><link rel="preload" href="/web/js/hexo-theme-yun.js" as="script"><link rel="prefetch" href="/web/js/sidebar.js" as="script"><link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin><script id="yun-config">
    const Yun = window.Yun || {};
    window.CONFIG = {"hostname":"zilongzeng.gitee.io","root":"/web/","title":"奏者的小站","version":"1.6.2","mode":"auto","copycode":true,"page":{"isPost":true},"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}.","hits":"${hits} results found","hits_time":"${hits} results found in ${time} ms"},"anonymous_image":"https://cdn.jsdelivr.net/gh/YunYouJun/cdn/img/avatar/none.jpg","say":{"api":"https://v1.hitokoto.cn","hitokoto":true},"fireworks":{"colors":["102, 167, 221","62, 131, 225","33, 78, 194"]}};
  </script><link rel="stylesheet" href="/web/css/hexo-theme-yun.css"><script src="/web/js/utils.js"></script><script src="/web/js/hexo-theme-yun.js"></script><meta name="description" content="java中级概念及知识点">
<meta property="og:type" content="article">
<meta property="og:title" content="java中级">
<meta property="og:url" content="http://zilongzeng.gitee.io/web/2022/10/07/java%E4%B8%AD%E7%BA%A7/index.html">
<meta property="og:site_name" content="zzl">
<meta property="og:description" content="java中级概念及知识点">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="http://zilongzeng.gitee.io/web/2022/10/07/java%E4%B8%AD%E7%BA%A7/image/java%E5%AF%B9%E8%B1%A1%E5%88%9B%E5%BB%BA%E5%86%85%E5%AD%98%E5%9B%BE.png">
<meta property="og:image" content="http://zilongzeng.gitee.io/web/2022/10/07/java%E4%B8%AD%E7%BA%A7/image/java%E7%9A%84JVM%E5%9B%BE01.png">
<meta property="og:image" content="http://zilongzeng.gitee.io/web/2022/10/07/java%E4%B8%AD%E7%BA%A7/image/java%E7%9A%84JVM%E5%9B%BE02.png">
<meta property="og:image" content="http://zilongzeng.gitee.io/web/2022/10/07/java%E4%B8%AD%E7%BA%A7/image/java%E7%9A%84JVM%E5%9B%BE03.png">
<meta property="og:image" content="http://zilongzeng.gitee.io/web/2022/10/07/java%E4%B8%AD%E7%BA%A7/image/java%E7%9A%84JVM%E5%9B%BE04.png">
<meta property="og:image" content="http://zilongzeng.gitee.io/web/2022/10/07/java%E4%B8%AD%E7%BA%A7/image/java%E7%9A%84JVM%E5%9B%BE05.png">
<meta property="og:image" content="http://zilongzeng.gitee.io/web/2022/10/07/java%E4%B8%AD%E7%BA%A7/image/java%E7%9A%84JVM%E5%9B%BE06.png">
<meta property="og:image" content="http://zilongzeng.gitee.io/web/2022/10/07/java%E4%B8%AD%E7%BA%A7/image/java%E7%9A%84JVM%E5%9B%BE07.png">
<meta property="article:published_time" content="2022-10-07T15:04:25.000Z">
<meta property="article:modified_time" content="2022-10-07T15:09:35.768Z">
<meta property="article:author" content="zzl">
<meta property="article:tag" content="java">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://zilongzeng.gitee.io/web/2022/10/07/java%E4%B8%AD%E7%BA%A7/image/java%E5%AF%B9%E8%B1%A1%E5%88%9B%E5%BB%BA%E5%86%85%E5%AD%98%E5%9B%BE.png"><script src="/web/js/ui/mode.js"></script></head><body><script defer src="https://cdn.jsdelivr.net/npm/animejs@latest"></script><script defer src="/web/js/ui/fireworks.js"></script><canvas class="fireworks"></canvas><div class="container"><a class="sidebar-toggle hty-icon-button" id="menu-btn"><div class="hamburger hamburger--spin" type="button"><span class="hamburger-box"><span class="hamburger-inner"></span></span></div></a><div class="sidebar-toggle sidebar-overlay"></div><aside class="sidebar"><script src="/web/js/sidebar.js"></script><ul class="sidebar-nav"><li class="sidebar-nav-item sidebar-nav-toc hty-icon-button sidebar-nav-active" data-target="post-toc-wrap" title="Table of Contents"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-list-ordered"></use></svg></li><li class="sidebar-nav-item sidebar-nav-overview hty-icon-button" data-target="site-overview-wrap" title="Overview"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-passport-line"></use></svg></li></ul><div class="sidebar-panel" id="site-overview-wrap"><div class="site-info fix-top"><a class="site-author-avatar" href="/web/about/" title="zzl"><img width="96" loading="lazy" src="/web/zzl.jpg" alt="zzl"><span class="site-author-status" title="永远相信美好的事情即将发生">😊</span></a><div class="site-author-name"><a href="/web/about/">zzl</a></div><span class="site-name">zzl</span><sub class="site-subtitle"></sub><div class="site-desciption"></div></div><nav class="site-state"><a class="site-state-item hty-icon-button icon-home" href="/web/" title="Home"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-home-4-line"></use></svg></span></a><div class="site-state-item"><a href="/web/archives/" title="Archives"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-archive-line"></use></svg></span><span class="site-state-item-count">16</span></a></div><div class="site-state-item"><a href="/web/categories/" title="Categories"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-2-line"></use></svg></span><span class="site-state-item-count">0</span></a></div><div class="site-state-item"><a href="/web/tags/" title="Tags"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="site-state-item-count">9</span></a></div><a class="site-state-item hty-icon-button" target="_blank" rel="noopener" href="https://yun.yunyoujun.cn" title="文档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-settings-line"></use></svg></span></a></nav><hr style="margin-bottom:0.5rem"><div class="links-of-author"><a class="links-of-author-item hty-icon-button" rel="noopener" href="/atom.xml" title="RSS" target="_blank" style="color:orange"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-rss-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="QQ 2915667803" target="_blank" style="color:#12B7F5"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-qq-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="GitHub" target="_blank" style="color:#6e5494"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-github-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="微博" target="_blank" style="color:#E6162D"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-weibo-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="豆瓣" target="_blank" style="color:#007722"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-douban-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="网易云音乐" target="_blank" style="color:#C20C0C"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-netease-cloud-music-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="知乎" target="_blank" style="color:#0084FF"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-zhihu-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="哔哩哔哩" target="_blank" style="color:#FF8EB3"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-bilibili-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="微信公众号" target="_blank" style="color:#1AAD19"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-wechat-2-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Twitter" target="_blank" style="color:#1da1f2"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-twitter-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Telegram Channel" target="_blank" style="color:#0088CC"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-telegram-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="E-Mail" target="_blank" style="color:#8E71C1"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-mail-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Travelling" target="_blank" style="color:var(--hty-text-color)"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-train-line"></use></svg></a></div><hr style="margin:0.5rem 1rem"><div class="links"><a class="links-item hty-icon-button" href="/web/links/" title="我的小伙伴们" style="color:dodgerblue"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-genderless-line"></use></svg></a></div><br><a class="links-item hty-icon-button" id="toggle-mode-btn" href="javascript:;" title="Mode" style="color: #f1cb64"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-contrast-2-line"></use></svg></a></div><div class="sidebar-panel sidebar-panel-active" id="post-toc-wrap"><div class="post-toc"><div class="post-toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#java-%E4%B8%AD%E7%BA%A7"><span class="toc-number">1.</span> <span class="toc-text">java 中级</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#java-%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1"><span class="toc-number">1.1.</span> <span class="toc-text">java 面向对象</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E4%B9%8B%E6%8A%BD%E8%B1%A1"><span class="toc-number">1.1.1.</span> <span class="toc-text">面向对象之抽象</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8A%BD%E8%B1%A1%E7%9A%84%E4%BB%A3%E7%A0%81%E5%B1%95%E7%A4%BA"><span class="toc-number">1.1.1.1.</span> <span class="toc-text">抽象的代码展示</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%B1%BB%E4%B8%AD%E7%9A%84%E6%88%90%E5%91%98%E5%8F%98%E9%87%8F%E5%92%8C%E5%B1%80%E5%8F%98%E9%87%8F"><span class="toc-number">1.1.1.2.</span> <span class="toc-text">类中的成员变量和局变量</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%B1%BB%E4%B8%AD%E7%9A%84%E6%96%B9%E6%B3%95"><span class="toc-number">1.1.1.3.</span> <span class="toc-text">类中的方法</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%E7%9A%84%E5%8F%82%E6%95%B0"><span class="toc-number">1.1.1.3.1.</span> <span class="toc-text">方法的参数</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%E7%9A%84%E8%BF%94%E5%9B%9E%E5%80%BC"><span class="toc-number">1.1.1.3.2.</span> <span class="toc-text">方法的返回值</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%E7%9A%84%E9%80%92%E5%BD%92"><span class="toc-number">1.1.1.3.3.</span> <span class="toc-text">方法的递归</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%E7%9A%84%E9%87%8D%E8%BD%BD"><span class="toc-number">1.1.1.3.4.</span> <span class="toc-text">方法的重载</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E4%B9%8B%E5%B0%81%E8%A3%85"><span class="toc-number">1.1.2.</span> <span class="toc-text">面向对象之封装</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#package-%E5%8C%85%E5%90%8D"><span class="toc-number">1.1.2.1.</span> <span class="toc-text">package 包名</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9E%84%E9%80%A0%E5%99%A8-constructor"><span class="toc-number">1.1.2.2.</span> <span class="toc-text">构造器 constructor</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#this-%E5%85%B3%E9%94%AE%E5%AD%97"><span class="toc-number">1.1.2.3.</span> <span class="toc-text">this 关键字</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#String-%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">1.1.2.4.</span> <span class="toc-text">String 的使用</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#String-%E4%B8%AD%E7%9A%84%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95"><span class="toc-number">1.1.2.4.1.</span> <span class="toc-text">String 中的常用方法</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%88%B6%E4%BD%9C-String-%E7%9A%84%E8%87%AA%E5%AE%9A%E4%B9%89%E5%B7%A5%E5%85%B7%E7%B1%BB"><span class="toc-number">1.1.2.4.2.</span> <span class="toc-text">制作 String 的自定义工具类</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%8B%93%E5%B1%95-String-%E7%9A%84%E5%AF%B9%E8%B1%A1%E5%88%9B%E5%BB%BA%E5%AD%98%E5%82%A8%E9%97%AE%E9%A2%98"><span class="toc-number">1.1.2.4.3.</span> <span class="toc-text">拓展 String 的对象创建存储问题</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%87%AA%E5%8A%A8%E6%8B%86%E8%A3%85%E7%AE%B1"><span class="toc-number">1.1.2.5.</span> <span class="toc-text">自动拆装箱</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%8B%86%E7%AE%B1%E5%92%8C%E8%A3%85%E7%AE%B1%E5%B1%95%E7%A4%BA"><span class="toc-number">1.1.2.5.1.</span> <span class="toc-text">拆箱和装箱展示</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%B0%81%E8%A3%85%E7%BB%83%E4%B9%A0"><span class="toc-number">1.1.2.6.</span> <span class="toc-text">封装练习</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%B0%81%E8%A3%85%E8%B6%85%E7%BA%A7%E6%95%B0%E7%BB%84"><span class="toc-number">1.1.2.6.1.</span> <span class="toc-text">封装超级数组</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E4%B9%8B%E7%BB%A7%E6%89%BF"><span class="toc-number">1.1.3.</span> <span class="toc-text">面向对象之继承</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BB%A7%E6%89%BF%E7%9A%84%E6%A6%82%E8%BF%B0"><span class="toc-number">1.1.3.1.</span> <span class="toc-text">继承的概述</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BB%A7%E6%89%BF%E7%AE%80%E5%8D%95%E7%A4%BA%E4%BE%8B"><span class="toc-number">1.1.3.2.</span> <span class="toc-text">继承简单示例</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AD%90%E7%88%B6%E7%B1%BB%E7%9A%84%E6%9E%84%E9%80%A0%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F"><span class="toc-number">1.1.3.3.</span> <span class="toc-text">子父类的构造执行顺序</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#super-%E5%85%B3%E9%94%AE%E5%AD%97"><span class="toc-number">1.1.3.4.</span> <span class="toc-text">super 关键字</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8-super-%E8%AE%BF%E9%97%AE%E7%88%B6%E7%B1%BB%E5%B1%9E%E6%80%A7%E5%92%8C%E6%96%B9%E6%B3%95"><span class="toc-number">1.1.3.4.1.</span> <span class="toc-text">使用 super 访问父类属性和方法</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%9C%A8%E6%9E%84%E9%80%A0%E4%B8%AD%E4%BD%BF%E7%94%A8-super-%E5%85%B3%E9%94%AE%E5%AD%97"><span class="toc-number">1.1.3.4.2.</span> <span class="toc-text">在构造中使用 super 关键字</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%85%B3%E9%94%AE%E5%AD%97-this-%E5%92%8C-super-%E6%80%BB%E7%BB%93"><span class="toc-number">1.1.3.4.3.</span> <span class="toc-text">关键字 this 和 super 总结</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%E7%9A%84%E9%87%8D%E5%86%99"><span class="toc-number">1.1.3.5.</span> <span class="toc-text">方法的重写</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%E7%9A%84%E9%87%8D%E5%86%99%E5%92%8C%E9%87%8D%E8%BD%BD%E5%8C%BA%E5%88%AB"><span class="toc-number">1.1.3.5.1.</span> <span class="toc-text">方法的重写和重载区别</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#final-%E5%85%B3%E9%94%AE%E5%AD%97"><span class="toc-number">1.1.3.6.</span> <span class="toc-text">final 关键字</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%B6%85%E7%B1%BB-Object"><span class="toc-number">1.1.3.7.</span> <span class="toc-text">超类 Object</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#hashCode"><span class="toc-number">1.1.3.7.1.</span> <span class="toc-text">hashCode</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#equals"><span class="toc-number">1.1.3.7.2.</span> <span class="toc-text">equals</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#toString-NaN"><span class="toc-number">1.1.3.7.3.</span> <span class="toc-text">toString</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#clone"><span class="toc-number">1.1.3.7.4.</span> <span class="toc-text">clone</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E4%B9%8B%E5%A4%9A%E6%80%81"><span class="toc-number">1.1.4.</span> <span class="toc-text">面向对象之多态</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%A4%9A%E6%80%81%E7%9A%84%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%86"><span class="toc-number">1.1.4.1.</span> <span class="toc-text">多态的底层原理</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%AD%97%E8%8A%82%E7%A0%81%E5%B1%82%E5%88%86%E6%9E%90"><span class="toc-number">1.1.4.1.1.</span> <span class="toc-text">字节码层分析</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%E5%9C%A8%E6%A0%88%E4%B8%AD%E7%9A%84%E8%B0%83%E7%94%A8"><span class="toc-number">1.1.4.1.2.</span> <span class="toc-text">方法在栈中的调用</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%87%8D%E8%BD%BD%E6%96%B9%E6%B3%95%E7%9A%84%E8%B0%83%E7%94%A8"><span class="toc-number">1.1.4.2.</span> <span class="toc-text">重载方法的调用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%87%8D%E5%86%99%E6%96%B9%E6%B3%95%E7%9A%84%E8%B0%83%E7%94%A8"><span class="toc-number">1.1.4.3.</span> <span class="toc-text">重写方法的调用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%87%8D%E5%86%99%E5%92%8C%E9%87%8D%E8%BD%BD"><span class="toc-number">1.1.4.4.</span> <span class="toc-text">重写和重载</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AF%B9%E8%B1%A1%E8%BD%AC%E5%9E%8B"><span class="toc-number">1.1.4.5.</span> <span class="toc-text">对象转型</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8A%BD%E8%B1%A1%E7%B1%BB%E5%92%8C%E6%8E%A5%E5%8F%A3"><span class="toc-number">1.2.</span> <span class="toc-text">抽象类和接口</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8A%BD%E8%B1%A1%E7%B1%BB"><span class="toc-number">1.2.1.</span> <span class="toc-text">抽象类</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8A%BD%E8%B1%A1%E7%B1%BB%E5%B0%8F%E7%BB%83%E4%B9%A0"><span class="toc-number">1.2.1.1.</span> <span class="toc-text">抽象类小练习</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8A%BD%E8%B1%A1%E7%B1%BB%E7%89%B9%E7%82%B9"><span class="toc-number">1.2.1.2.</span> <span class="toc-text">抽象类特点</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8E%A5%E5%8F%A3"><span class="toc-number">1.2.2.</span> <span class="toc-text">接口</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8E%A5%E5%8F%A3%E5%B0%8F%E7%BB%83%E4%B9%A0"><span class="toc-number">1.2.2.1.</span> <span class="toc-text">接口小练习</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8E%A5%E5%8F%A3%E7%89%B9%E7%82%B9"><span class="toc-number">1.2.2.2.</span> <span class="toc-text">接口特点</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%B0%E5%BA%95%E6%98%AF%E6%8E%A5%E5%8F%A3%E8%BF%98%E6%98%AF%E6%8A%BD%E8%B1%A1%E7%B1%BB%EF%BC%9F"><span class="toc-number">1.2.3.</span> <span class="toc-text">到底是接口还是抽象类？</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%85%B6%E4%BB%96%E7%9F%A5%E8%AF%86"><span class="toc-number">1.3.</span> <span class="toc-text">面向对象的其他知识</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#java-%E4%B8%AD%E7%9A%84%E5%80%BC%E4%BC%A0%E9%80%92"><span class="toc-number">1.3.1.</span> <span class="toc-text">java 中的值传递</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#java-main-%E6%96%B9%E6%B3%95"><span class="toc-number">1.3.2.</span> <span class="toc-text">java main 方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#java-%E7%9A%84%E9%9D%99%E6%80%81"><span class="toc-number">1.3.3.</span> <span class="toc-text">java 的静态</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%9D%99%E6%80%81%E4%BB%A3%E7%A0%81%E5%9D%97"><span class="toc-number">1.3.3.1.</span> <span class="toc-text">静态代码块</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%9D%99%E6%80%81%E4%BB%A3%E7%A0%81%E5%9D%97%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F"><span class="toc-number">1.3.3.2.</span> <span class="toc-text">静态代码块执行顺序</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#java-%E4%B8%AD%E7%9A%84-JVM"><span class="toc-number">1.3.4.</span> <span class="toc-text">java 中的 JVM</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%98%AF%E8%A7%A3%E9%87%8A%E8%BF%98%E6%98%AF%E7%BC%96%E8%AF%91%EF%BC%9F"><span class="toc-number">1.3.4.1.</span> <span class="toc-text">是解释还是编译？</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8A%A8%E6%80%81%E8%BF%98%E6%98%AF%E9%9D%99%E6%80%81%EF%BC%9F"><span class="toc-number">1.3.4.2.</span> <span class="toc-text">动态还是静态？</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%B1%BB%E7%9A%84%E5%8A%A0%E8%BD%BD"><span class="toc-number">1.3.4.3.</span> <span class="toc-text">类的加载</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%B1%BB%E5%8A%A0%E8%BD%BD%E5%99%A8"><span class="toc-number">1.3.4.3.1.</span> <span class="toc-text">类加载器</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E7%AE%97%E6%B3%95"><span class="toc-number">1.3.4.4.</span> <span class="toc-text">垃圾回收算法</span></a></li></ol></li></ol></li></ol></li></ol></div></div></div></aside><main class="sidebar-translate" id="content"><div id="post"><article class="hty-card post-block" itemscope itemtype="https://schema.org/Article"><link itemprop="mainEntityOfPage" href="http://zilongzeng.gitee.io/web/web/2022/10/07/java%E4%B8%AD%E7%BA%A7/"><span hidden itemprop="author" itemscope itemtype="https://schema.org/Person"><meta itemprop="name" content="zzl"><meta itemprop="description"></span><span hidden itemprop="publisher" itemscope itemtype="https://schema.org/Organization"><meta itemprop="name" content="zzl"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">java中级</h1><div class="post-meta"><div class="post-time" style="display:inline-block"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-line"></use></svg></span> <time title="Created: 2022-10-07 23:04:25" itemprop="dateCreated datePublished" datetime="2022-10-07T23:04:25+08:00">2022-10-07</time></div><div class="post-classify"><span class="post-tag"><a class="tag-item" href="/web/tags/java/" style="--text-color:var(--hty-text-color)"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">java</span></a></span></div></div></header><section class="post-body" itemprop="articleBody"><div class="post-content markdown-body" style="--smc-primary:#0078E7;"><h1 id="java-中级"><a href="#java-中级" class="headerlink" title="java 中级"></a>java 中级</h1><h2 id="java-面向对象"><a href="#java-面向对象" class="headerlink" title="java 面向对象"></a>java 面向对象</h2><ul>
<li><p>java 是一门面向对象的语言，在java里面 一切皆对象 </p>
</li>
<li><p>面向对象的常见名词</p>
<ul>
<li>OOA：面向对象的分析</li>
<li>OOD：面向对象的设计</li>
<li>OOP：面向对象的编程实现</li>
</ul>
</li>
<li><p>首先要明白，面向过程和面向对象</p>
<ul>
<li>什么是面向过程？<ul>
<li>面向过程就是将问题的所有步骤都分析出来</li>
<li>当需要实现某一个功能时，就要把每一个步骤都详细的写出来</li>
<li>PS：C语言就是一个面向过程的语句，通过函数一步一步实现的</li>
</ul>
</li>
<li>什么是面向对象？<ul>
<li>面向对象就是把整个需求按照特点、功能进行划分，将其共同特性封装成类(类型)，通过生成对应的对象(实例)解决问题</li>
<li>当需要使用一个功能时，不必关系具体的实现，而是找到一个具有该功能的类的对象来帮我们做事(当然，我们是程序员，有时候也需要创建)</li>
<li>PS：类和对象之间的关系，相当于，一个是设计图，一个是成品</li>
</ul>
</li>
</ul>
</li>
<li><p>就比如之前使用的 Scanner ，它可以接收用户在控制台输入的信息。我们使用时不需要考虑它是如何实现该功能的，对我们来说只要能够帮我们接收用户在控制台输入的信息就好了</p>
</li>
<li><p>其实我们之前的代码，大多都是面向过程</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">int</span>[] arr = &#123;<span class="number">5</span>, <span class="number">2</span>, <span class="number">12</span>, <span class="number">57</span>, <span class="number">11</span>, <span class="number">59</span>, <span class="number">25</span>, <span class="number">20</span>&#125;;</span><br><span class="line">    <span class="comment">// 从小到大排序</span></span><br><span class="line">    <span class="comment">// 外层循环控制元素移动的次数</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length - <span class="number">1</span>; i++) &#123;</span><br><span class="line">        <span class="comment">// 内存循环将元素一个个放置数组的尾部</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; arr.length - <span class="number">1</span> - i; j++) &#123;</span><br><span class="line">            <span class="comment">// 比较前后两个元素的大小</span></span><br><span class="line">            <span class="comment">// arr[j] VS arr[j+1]</span></span><br><span class="line">            <span class="keyword">if</span> (arr[j] &gt; arr[j + <span class="number">1</span>]) &#123;</span><br><span class="line">                <span class="comment">// 大的元素在小的元素后面</span></span><br><span class="line">                <span class="keyword">int</span> temp = arr[j];</span><br><span class="line">                arr[j] = arr[j + <span class="number">1</span>];</span><br><span class="line">                arr[j + <span class="number">1</span>] = temp;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>面向对象的特点</p>
<ul>
<li>面向对象的思想是一种更加符合我们思考习惯的思想</li>
<li>它能将一个复杂事情简单化，将我们从执行者变成了指挥者</li>
<li>面向对象的特点可以分为以下四点：抽象，封装，继承，多态</li>
</ul>
</li>
</ul>
<h3 id="面向对象之抽象"><a href="#面向对象之抽象" class="headerlink" title="面向对象之抽象"></a>面向对象之抽象</h3><ul>
<li>抽象就是将一类对象的 <strong>共同特征</strong> 总结出来构建 <strong>类</strong> 的过程，包括 <strong>数据抽象(静态特征，属性)</strong> 和 <strong>行为抽象(动态特征，方法)</strong> 两个方面。抽象只需要关注对象的哪些属性和方法，不需要关注这些行为的细节是什么</li>
<li>类 和 对象的区别<ul>
<li>类代表一个事物的抽象(不存在)</li>
<li>对象是真实存在的(具体的)</li>
<li>类是对象的设计图，设计出来后的实物就是对象</li>
</ul>
</li>
<li>抽象类和对象(实例)<ul>
<li>类：一组相关 属性 和 方法 的集合<ul>
<li>属性：事物的状态描述</li>
<li>方法：事物能够做的事情</li>
</ul>
</li>
<li>例如：猫类<ul>
<li>属性：名字(name)，年龄(age)，颜色(color)，体重(weight)</li>
<li>方法：吃(eat)，睡觉(sleep)，卖萌(actCute)</li>
</ul>
</li>
<li>对象实例：<ul>
<li>属性：tom，6，blue，5kg</li>
<li>方法：吃罐头，倒头就睡，可怜巴巴</li>
</ul>
</li>
</ul>
</li>
</ul>
<h4 id="抽象的代码展示"><a href="#抽象的代码展示" class="headerlink" title="抽象的代码展示"></a>抽象的代码展示</h4><ul>
<li><p>类创建的语法格式</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">package</span> 所在包的位置，从 src 开始;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> 导包语句，导入需要使用的其他类的位置，如果没有可以省略;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> 类名</span>&#123;</span><br><span class="line">    <span class="comment">// 属性也是变量</span></span><br><span class="line">    属性类型 属性名;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> 方法名()&#123;&#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>类抽象的实例代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">package</span> com.seecen.day05;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@program</span>: javaSE</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>:</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: lan</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@create</span>: 2022-07-12 17:32</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span>:1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Cat</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 数据抽象，属性：</span></span><br><span class="line">    <span class="comment">// 名字属性</span></span><br><span class="line">    String name;</span><br><span class="line">    <span class="comment">// 年龄属性</span></span><br><span class="line">    <span class="keyword">int</span> age;</span><br><span class="line">    <span class="comment">// 颜色属性</span></span><br><span class="line">    String color;</span><br><span class="line">    <span class="comment">// 体重属性</span></span><br><span class="line">    <span class="keyword">double</span> weight;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 行为抽象，方法：</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 猫吃东西的方法</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">eat</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;猫在吃罐头&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 猫睡觉的方法</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sleep</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;猫倒头就睡&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 猫卖萌的方法</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actCute</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;猫可怜巴巴的样子&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>使用类创建对象的步骤：</p>
<ol>
<li>导包：也就是指出类的所在位置，以 src 开始<ul>
<li>import 包名.类名;</li>
<li><code>import com.seecen.day05.Cat;</code></li>
</ul>
</li>
<li>使用 new 关键字创建实例<ul>
<li>类名 对象名 = new 类名();</li>
<li><code>Cat cat = new Cat();</code></li>
</ul>
</li>
<li>访问属性或方法<ul>
<li>对象名.属性名;</li>
<li>对象名.方法名(参数);</li>
<li>想用什么就直接通过：对象名. 进行调用</li>
</ul>
</li>
</ol>
</li>
<li><p>使用类创建对象的示例代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">package</span> com.seecen.day05;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 1，导包</span></span><br><span class="line"><span class="keyword">import</span> com.seecen.day05.Cat;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@program</span>: javaSE</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>:</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: lan</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@create</span>: 2022-07-12 17:41</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span>:1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test01</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 2，使用 new 关键字创建对象</span></span><br><span class="line">        Cat cat = <span class="keyword">new</span> Cat();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 3，访问对象的属性和方法，通过 . 方法问</span></span><br><span class="line">        <span class="comment">// 访问属性，给属性赋值</span></span><br><span class="line">        cat.name = <span class="string">&quot;tom&quot;</span>;</span><br><span class="line">        cat.age = <span class="number">6</span>;</span><br><span class="line">        cat.color = <span class="string">&quot;blue&quot;</span>;</span><br><span class="line">        cat.weight = <span class="number">5.0</span>;</span><br><span class="line">        <span class="comment">// 访问属性，获取属性值</span></span><br><span class="line">        System.out.println(<span class="string">&quot;猫的名字是：&quot;</span>+cat.name);</span><br><span class="line">        System.out.println(<span class="string">&quot;猫的年龄是：&quot;</span>+cat.age);</span><br><span class="line">        System.out.println(<span class="string">&quot;猫的颜色是：&quot;</span>+cat.color);</span><br><span class="line">        System.out.println(<span class="string">&quot;猫的体重是：&quot;</span>+cat.weight+<span class="string">&quot;kg&quot;</span>);</span><br><span class="line">        <span class="comment">// 访问方法</span></span><br><span class="line">        cat.eat();</span><br><span class="line">        cat.sleep();</span><br><span class="line">        cat.actCute();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<ul>
<li><p>对象创建内存图</p>
<p><img src="image/java%E5%AF%B9%E8%B1%A1%E5%88%9B%E5%BB%BA%E5%86%85%E5%AD%98%E5%9B%BE.png" loading="lazy"></p>
</li>
</ul>
<h4 id="类中的成员变量和局变量"><a href="#类中的成员变量和局变量" class="headerlink" title="类中的成员变量和局变量"></a>类中的成员变量和局变量</h4><ul>
<li><p>成员变量是书写在类中的，方法外</p>
</li>
<li><p>局部变量是书写在方法中的</p>
</li>
<li><p>成员变量和局部变量的区别</p>
<ul>
<li>定义的位置不同<ul>
<li>成员变量：在方法以外，直接写在类中</li>
<li>局部变量：在方法的内部</li>
</ul>
</li>
<li>作用范围不同<ul>
<li>成员变量：整个类中都可以使用</li>
<li>局部变量：只有在方法的内部有效，出了方法就无效了</li>
</ul>
</li>
<li>默认值不同<ul>
<li>成员变量：有默认值，和数组的默认值规则一样</li>
<li>局部变量：没有默认值，使用之前必须赋值</li>
</ul>
</li>
<li>内存位置不同<ul>
<li>成员变量：存储在堆中</li>
<li>局部变量：存储在栈中</li>
</ul>
</li>
<li>生命周期不同<ul>
<li>成员变量：随对象的创建而诞生，随对象垃圾回收而消失</li>
<li>局部变量：随方法的进栈而诞生，随方法的出栈而消失</li>
</ul>
</li>
</ul>
</li>
<li><p>代码展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">package</span> com.seecen.day06;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@program</span>: javaSE</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>:</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: lan</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@create</span>: 2022-07-13 11:11</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span>:1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 成员变量</span></span><br><span class="line">    <span class="comment">// 物种</span></span><br><span class="line">    String species;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 成员方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">who</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">// 局部变量</span></span><br><span class="line">        String species = <span class="string">&quot;老虎&quot;</span>;</span><br><span class="line">        <span class="keyword">int</span> age = <span class="number">16</span>;</span><br><span class="line">        <span class="keyword">int</span> age2 = <span class="number">16</span>;</span><br><span class="line">        System.out.println(<span class="string">&quot;这是什么？&quot;</span>+age+<span class="string">&quot;岁的&quot;</span>+species);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 参数也属于局部变量</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> age</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">who2</span><span class="params">(<span class="keyword">int</span> age,String species)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 不能创建与参数名相同的局部变量</span></span><br><span class="line"><span class="comment">//        int age = 18;</span></span><br><span class="line">        System.out.println(<span class="string">&quot;这是什么？&quot;</span>+age+<span class="string">&quot;岁的&quot;</span>+species);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="类中的方法"><a href="#类中的方法" class="headerlink" title="类中的方法"></a>类中的方法</h4><ul>
<li><p>普通方法的语法格式</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">访问修饰符 返回值类型 方法名(参数列表)&#123;</span><br><span class="line">    执行代码</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">实例代码：</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">who2</span><span class="params">(<span class="keyword">int</span> age,String species)</span></span>&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;这是什么？&quot;</span>+age+<span class="string">&quot;岁的&quot;</span>+species);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">说明：</span><br><span class="line">    <span class="keyword">public</span>：访问修饰符，学习封装时会详细讲解</span><br><span class="line">    <span class="keyword">void</span>：返回值类型，<span class="keyword">void</span> 表示为没有返回值，可以替换成 <span class="keyword">int</span> String 等等...</span><br><span class="line">    ()：参数列表，可以为什么都不写，也可以为单个或多个，多个参数用 , 分隔</span><br><span class="line">    	参数书写：类型 变量名</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h5 id="方法的参数"><a href="#方法的参数" class="headerlink" title="方法的参数"></a>方法的参数</h5><ul>
<li><p>在方法名后面的括号中声明的变量，就是方法的参数，参数可以分为形参和实参。任意数据类型都可以作为参数</p>
<ul>
<li>形参：创建方法时，只有类型没有值的变量</li>
<li>实参：调用方法时，有值的量</li>
<li>PS：形参的名称可以不需要和实参一致，可以任意定义；引用类型作为实参时，传递的是一个引用地址值</li>
</ul>
</li>
<li><p>代码展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">package</span> com.seecen.day06;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@program</span>: javaSE</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>:</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: lan</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@create</span>: 2022-07-13 14:11</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span>:1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Phone</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 成员变量，属性</span></span><br><span class="line">    <span class="comment">// 品牌</span></span><br><span class="line">    String brand;</span><br><span class="line">    <span class="comment">// 颜色</span></span><br><span class="line">    String color;</span><br><span class="line">    <span class="comment">// 金额</span></span><br><span class="line">    <span class="keyword">double</span> price;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 方法</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 使用手机打电话的方法</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> name 指定打给谁</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">call</span><span class="params">(String name)</span></span>&#123;      </span><br><span class="line">        System.out.println(<span class="string">&quot;使用&quot;</span>+brand+<span class="string">&quot;牌子的手机给&quot;</span>+name+<span class="string">&quot;打电话&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 使用手机发送信息的方法</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> name 指定发给谁</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> msg 发送信息的内容</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sendMsg</span><span class="params">(String name,String msg)</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;使用&quot;</span>+brand+<span class="string">&quot;牌子的手机给&quot;</span>+name+<span class="string">&quot;发送信息。&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;信息内容为：&quot;</span>+msg);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 使用手机玩游戏的方法</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> game 游戏的名字</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> time 玩游戏的时间，单位：小时</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">playGame</span><span class="params">(String game,<span class="keyword">int</span> time)</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;使用&quot;</span>+brand+<span class="string">&quot;牌子的手机玩 \&quot;&quot;</span>+game+<span class="string">&quot;\&quot; 游戏&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;一共玩了&quot;</span>+time*<span class="number">60</span>+<span class="string">&quot;分钟&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 手机群发短信的方法</span></span><br><span class="line"><span class="comment">     *  如果有一个不确定数量的参数可是使用 ... 的形式表示零个或多个</span></span><br><span class="line"><span class="comment">     *  该参数为：可变长度参数列表，使用时可以作为一个数组使用</span></span><br><span class="line"><span class="comment">     *  注意！使用可变长参数列表必须放在参数列表的末尾</span></span><br><span class="line"><span class="comment">     *      并且一个方法只能有一个可变参数列表</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> names 接收短信人的名单列表，可以不写</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> msg 短信的内容</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">massMsg</span><span class="params">(String msg,String...names)</span></span>&#123;</span><br><span class="line">        System.out.print(<span class="string">&quot;使用&quot;</span>+brand+<span class="string">&quot;牌子的手机发送给：&quot;</span>);</span><br><span class="line">        <span class="comment">// 使用增强 for 循环遍历数组</span></span><br><span class="line">        <span class="comment">// name 就是每次循环获取的 names 中的元素</span></span><br><span class="line">        <span class="keyword">for</span> (String name : names) &#123;</span><br><span class="line">            System.out.print(name + <span class="string">&quot; &quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;信息内容为：&quot;</span>+msg);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h5 id="方法的返回值"><a href="#方法的返回值" class="headerlink" title="方法的返回值"></a>方法的返回值</h5><ul>
<li><p>在创建方法时就会固定其返回值类型，如果返回值类型不是 void，那么在方法中必须使用 return 关键字进行相应的返回。return 后续的代码不执行，直接返回结果，任何的数据类型都可以作为返回值类型。引用类型返回引用地址，基本类型返回字面值</p>
</li>
<li><p>代码展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">package</span> com.seecen.day06;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@program</span>: javaSE</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>:</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: lan</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@create</span>: 2022-07-13 14:11</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span>:1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Phone</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 成员变量，属性</span></span><br><span class="line">    <span class="comment">// 品牌</span></span><br><span class="line">    String brand;</span><br><span class="line">    <span class="comment">// 颜色</span></span><br><span class="line">    String color;</span><br><span class="line">    <span class="comment">// 金额</span></span><br><span class="line">    <span class="keyword">double</span> price;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 方法</span></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 手机看电影的方法</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> time 看电影的时间点，当前的时间点，24小时制</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">watchMovie</span><span class="params">(<span class="keyword">int</span> time)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 如果在上班期间就不能看点</span></span><br><span class="line">        <span class="keyword">if</span>(time&gt;<span class="number">9</span> &amp;&amp; time&lt;<span class="number">17</span>)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;上班期间，不允许摸鱼！&quot;</span>);</span><br><span class="line">            <span class="comment">// 在这里可以终止方法的运行，后续代码不需要执行，否则还是会看电影</span></span><br><span class="line">            <span class="comment">// 在没有返回值类型的方法中使用 return 关键字可以终止后续代码的执行</span></span><br><span class="line">            <span class="comment">// 注意！void 方法中的 return 的后面不能写返回值！</span></span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;电影针不戳！&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 使用手机打电话的方法</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> name 指定打给谁</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> time 指定打电话的时间，单位：分钟</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">call</span><span class="params">(String name,<span class="keyword">int</span> time)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 根据用户传递的时间进行判断</span></span><br><span class="line">        <span class="keyword">if</span>(time &lt;= <span class="number">30</span>)&#123;</span><br><span class="line">            <span class="comment">// 通话时间小于 30 分钟才打电话</span></span><br><span class="line">            System.out.println(<span class="string">&quot;使用&quot;</span>+brand+<span class="string">&quot;牌子的手机给&quot;</span>+name+</span><br><span class="line">                    <span class="string">&quot;打了&quot;</span>+time+<span class="string">&quot;分钟的电话&quot;</span>);</span><br><span class="line">            <span class="keyword">boolean</span> b = <span class="keyword">true</span>;</span><br><span class="line">            <span class="keyword">return</span> b;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 没有满足小于 30 分钟通话要求，则不能打电话</span></span><br><span class="line">        System.out.println(<span class="string">&quot;您想打&quot;</span>+time+<span class="string">&quot;分钟的电话？话费不够啊！&quot;</span>);</span><br><span class="line">        <span class="comment">// 因为 if 中的代码有可能不执行，但是有返回值的方法必须使用 return 关键字</span></span><br><span class="line">        <span class="comment">// 所以这里需要写一个 return 进行返回</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        <span class="comment">// return 的后续代码是不会执行的</span></span><br><span class="line"><span class="comment">//        System.out.println(&quot;这里会报错&quot;);</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 使用手机发送信息的方法</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> name 指定发给谁</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> msg 发送信息的内容</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sendMsg</span><span class="params">(String name,String msg)</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;使用&quot;</span>+brand+<span class="string">&quot;牌子的手机给&quot;</span>+name+<span class="string">&quot;发送信息。&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;信息内容为：&quot;</span>+msg);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 使用手机玩游戏的方法</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> game 游戏的名字</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> time 玩游戏的时间，单位：小时</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">playGame</span><span class="params">(String game,<span class="keyword">int</span> time)</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;使用&quot;</span>+brand+<span class="string">&quot;牌子的手机玩 \&quot;&quot;</span>+game+<span class="string">&quot;\&quot; 游戏&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;一共玩了&quot;</span>+time*<span class="number">60</span>+<span class="string">&quot;分钟&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 手机群发短信的方法</span></span><br><span class="line"><span class="comment">     *  如果有一个不确定数量的参数可是使用 ... 的形式表示零个或多个</span></span><br><span class="line"><span class="comment">     *  该参数为：可变长度参数列表，使用时可以作为一个数组使用</span></span><br><span class="line"><span class="comment">     *  注意！使用可变长参数列表必须放在参数列表的末尾</span></span><br><span class="line"><span class="comment">     *      并且一个方法只能有一个可变参数列表</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> names 接收短信人的名单列表，可以不写</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> msg 短信的内容</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">massMsg</span><span class="params">(String msg,String...names)</span></span>&#123;</span><br><span class="line">        System.out.print(<span class="string">&quot;使用&quot;</span>+brand+<span class="string">&quot;牌子的手机发送给：&quot;</span>);</span><br><span class="line">        <span class="comment">// 使用增强 for 循环遍历数组</span></span><br><span class="line">        <span class="comment">// name 就是每次循环获取的 names 中的元素</span></span><br><span class="line">        <span class="keyword">for</span> (String name : names) &#123;</span><br><span class="line">            System.out.print(name + <span class="string">&quot; &quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;信息内容为：&quot;</span>+msg);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h5 id="方法的递归"><a href="#方法的递归" class="headerlink" title="方法的递归"></a>方法的递归</h5><ul>
<li><p>递归是一种应用广泛的算法(或者编程技巧)。递归求解问题的分解过程，去的过程叫做 “递” ，回来的过程叫做 “归”</p>
</li>
<li><p>递归需要满足的条件：</p>
<ul>
<li>可以通过递归调用来缩小问题的规模，且新问题与原问题有着相同的形式。(自身调用)</li>
<li>存在一种简单情景，可以使递归在简单情景下退出。(递归出口)</li>
</ul>
</li>
<li><p>代码展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">package</span> com.seecen.day06;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@program</span>: javaSE</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>:</span></span><br><span class="line"><span class="comment"> *  递归</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: lan</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@create</span>: 2022-07-13 16:50</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span>:1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Recursion</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 打印数组的方法</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> arr 需要打印的数组</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">printIntArray</span><span class="params">(<span class="keyword">int</span>[] arr)</span></span>&#123;</span><br><span class="line">        System.out.print(<span class="string">&quot;[&quot;</span>);</span><br><span class="line">        <span class="comment">// 通过循环打印数组</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (i == arr.length - <span class="number">1</span>)&#123;</span><br><span class="line">                System.out.print(arr[i]);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.print(arr[i]+<span class="string">&quot;, &quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;]&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 打印二维数组</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> arr 需要打印的二维数组</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">printDoubleIntArray</span><span class="params">(<span class="keyword">int</span>[][] arr)</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;&#123;&quot;</span>);</span><br><span class="line">        <span class="comment">// 通过循环打印数组</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="comment">// 做的就是打印数组</span></span><br><span class="line">            printIntArray(arr[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;&#125;&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 一个方法在方法体中调用自身，如果没有合理的退出情况会出现错误</span></span><br><span class="line"><span class="comment">     *  StackOverflowError：栈溢出异常</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sleep</span><span class="params">(<span class="keyword">int</span> count)</span></span>&#123;</span><br><span class="line">        count++;</span><br><span class="line">        <span class="keyword">if</span>(count&gt;<span class="number">5</span>)&#123;</span><br><span class="line">            <span class="comment">// 如果计数次数超过 5 则结束方法</span></span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;我要一直睡觉&quot;</span>);</span><br><span class="line">        <span class="comment">// 递归睡觉不能超过五次</span></span><br><span class="line">        sleep(count);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 斐波那契数列（Fibonacci sequence），又称黄金分割数列，又称为 “兔子数列”</span></span><br><span class="line"><span class="comment">     *  指的是这样一个数列：1、1、2、3、5、8、13、21、34。。。</span></span><br><span class="line"><span class="comment">     *  这个数列从第3项开始，每一项都等于前两项之和</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> num 指定需要返回数列中的第几个数字</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 返回指定位置的数字值</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">fibonacci</span><span class="params">(<span class="keyword">int</span> num)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 如果是第一个和第二个，直接返回 1</span></span><br><span class="line">        <span class="keyword">if</span>(num == <span class="number">1</span> || num ==<span class="number">2</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 如果传入的值为 1 或 2 则不会继续调用</span></span><br><span class="line">        <span class="keyword">return</span> fibonacci(num-<span class="number">1</span>)+fibonacci(num-<span class="number">2</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">    * 斐波那契数列具体实现思路</span></span><br><span class="line"><span class="comment">    *                               fibonacci(5) = 3+2 = 5</span></span><br><span class="line"><span class="comment">    *                  3=fibonacci(4)     +       fibonacci(3) = 2</span></span><br><span class="line"><span class="comment">    *       2=fibonacci(3) + fibonacci(2)   fibonacci(2) + fibonacci(1)</span></span><br><span class="line"><span class="comment">    *fibonacci(2) + fibonacci(1)    1              1       +     1</span></span><br><span class="line"><span class="comment">    *   1         +    1</span></span><br><span class="line"><span class="comment">    * */</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h5 id="方法的重载"><a href="#方法的重载" class="headerlink" title="方法的重载"></a>方法的重载</h5><ul>
<li><p>方法的重载(Overload)</p>
<ul>
<li><p>对于希望完成一个功能，但是操作的数据不同时。我们希望有一个方法可以解决所有问题，而不是需要记住处理的每一个方法</p>
</li>
<li><p>在本类中，方法名一致，参数列表不一致，与返回值类型和访问修饰符无关。都称之为重载(Overload)包括构造方法和普通方法</p>
</li>
<li><p>参数列表不一致(参数的类型，参数的个数，参数的顺序)，与参数名无关</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">a</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">double</span> b)</span></span>&#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">a</span><span class="params">(String a,<span class="keyword">double</span> b)</span></span>&#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">a</span><span class="params">(<span class="keyword">double</span> b,<span class="keyword">int</span> a)</span></span>&#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 这个就不是重载！！！</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">a</span><span class="params">(<span class="keyword">int</span> abc,<span class="keyword">double</span> bcy)</span></span>&#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>我们使用的 System.out.print() 就是一个标准的重载方法</p>
<ul>
<li><p>参考 System.out.print() 编写一个自己的打印类可以打印八大基本类型加上String</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyPrint</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">println</span><span class="params">(String str)</span></span>&#123;</span><br><span class="line">        System.out.println(str);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">println</span><span class="params">(<span class="keyword">byte</span> b)</span></span>&#123;</span><br><span class="line">        System.out.println(b);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">println</span><span class="params">(<span class="keyword">short</span> s)</span></span>&#123;</span><br><span class="line">        System.out.println(s);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">println</span><span class="params">(<span class="keyword">int</span> i)</span></span>&#123;</span><br><span class="line">        System.out.println(i);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">println</span><span class="params">(<span class="keyword">long</span> l)</span></span>&#123;</span><br><span class="line">        System.out.println(l);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">println</span><span class="params">(<span class="keyword">float</span> f)</span></span>&#123;</span><br><span class="line">        System.out.println(f);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">println</span><span class="params">(<span class="keyword">double</span> d)</span></span>&#123;</span><br><span class="line">        System.out.println(d);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">println</span><span class="params">(<span class="keyword">char</span> c)</span></span>&#123;</span><br><span class="line">        System.out.println(c);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">println</span><span class="params">(<span class="keyword">boolean</span> b)</span></span>&#123;</span><br><span class="line">        System.out.println(b);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">println</span><span class="params">(<span class="keyword">int</span>[] arr)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 打印数组</span></span><br><span class="line">        System.out.print(<span class="string">&quot;[&quot;</span>);</span><br><span class="line">        <span class="comment">// 通过循环打印数组</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (i == arr.length - <span class="number">1</span>)&#123;</span><br><span class="line">                System.out.print(arr[i]);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.print(arr[i]+<span class="string">&quot;, &quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;]&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>练习：定义一个算数类(MyMath)，可以完成数字之间的加法运算(add)，会返回运算结果</p>
<ul>
<li><p>能够完成 int 和 int 之间的加法运算</p>
</li>
<li><p>能够完成 int 和 double 之间的加法运算</p>
</li>
<li><p>能够完成 double 和 int 之间的加法运算</p>
</li>
<li><p>能够完成 double 和 double 之间的加法运算</p>
</li>
<li><p>能够完成多个 int 之间的加法运算</p>
</li>
<li><p>能够完成多个 double 之间的加法运算</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyMath</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b)</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> a+b;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">double</span> b)</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> a+b;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">add</span><span class="params">(<span class="keyword">double</span> a,<span class="keyword">double</span> b)</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> a+b;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">add</span><span class="params">(<span class="keyword">double</span> a,<span class="keyword">int</span> b)</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> a+b;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span>...nums)</span></span>&#123;</span><br><span class="line">        <span class="keyword">int</span> num = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; nums.length; i++) &#123;</span><br><span class="line">            num = num + nums[i];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> num;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">add</span><span class="params">(<span class="keyword">double</span>...nums)</span></span>&#123;</span><br><span class="line">        <span class="keyword">double</span> num = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; nums.length; i++) &#123;</span><br><span class="line">            num = num + nums[i];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> num;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>
<h3 id="面向对象之封装"><a href="#面向对象之封装" class="headerlink" title="面向对象之封装"></a>面向对象之封装</h3><ul>
<li><p>将类的某些信息隐藏在类的内部，不允许外部程序直接访问，而是通过该类提供的方法来对隐藏的信息进行操作与访问。适当的封装可以让程序代码更容易理解与维护，也加强了代码的安全性</p>
<ul>
<li>方法就是一种封装</li>
<li>private(私有的)也是一种封装</li>
</ul>
</li>
<li><p><strong>实现封装的方式：使用访问修饰符</strong></p>
<table>
<thead>
<tr>
<th align="center">访问修饰符</th>
<th align="center">本类</th>
<th align="center">同包</th>
<th align="center">子类</th>
<th align="center">其他包</th>
</tr>
</thead>
<tbody><tr>
<td align="center">public(公开的)</td>
<td align="center">√</td>
<td align="center">√</td>
<td align="center">√</td>
<td align="center">√</td>
</tr>
<tr>
<td align="center">protected(受保护的)</td>
<td align="center">√</td>
<td align="center">√</td>
<td align="center">√</td>
<td align="center">×</td>
</tr>
<tr>
<td align="center">default(默认的，不写)</td>
<td align="center">√</td>
<td align="center">√</td>
<td align="center">×</td>
<td align="center">×</td>
</tr>
<tr>
<td align="center">private(私有的)</td>
<td align="center">√</td>
<td align="center">×</td>
<td align="center">×</td>
<td align="center">×</td>
</tr>
</tbody></table>
<ul>
<li>权限访问修饰符可以自己尝试用，注意！局部变量不能使用访问修饰符</li>
<li>class 类只能被 public 或 default 修饰，但是内部类支持所有的访问修饰符，后面会讲。使用 public class 修饰的类名必须与文件名相同</li>
</ul>
</li>
<li><p>封装的具体实现</p>
<ul>
<li>属性：<ul>
<li>修改属性的可见性，设置为 private</li>
<li>给所有的属性创建 getter/setter 方法(用于属性的读写)</li>
<li>在 getter/setter 方法中加入属性控制语句，对属性的合法性进行判断</li>
</ul>
</li>
<li>方法：<ul>
<li>修改方法的可见性，设置为 private</li>
<li>在非 private 方法中进行调用，完整一个完整的功能</li>
</ul>
</li>
</ul>
</li>
<li><p>java 的标准 bean （数据的存储）</p>
<ul>
<li>所有属性都是 private</li>
<li>提供无参构造</li>
<li>提供该类熟悉的 getter 和 setter 方法</li>
<li>实现一个 Serializable 接口</li>
</ul>
</li>
<li><p>代码展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Teacher</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 编号属性：1000-9999</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> tid;</span><br><span class="line">    <span class="comment">// 名字属性：没有要求</span></span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="comment">// 性别属性：男 或 女</span></span><br><span class="line">    <span class="keyword">private</span> String sex;</span><br><span class="line">    <span class="comment">// 年龄属性：26-65</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> age;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 这个方法说明了老师的一天生活</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">life</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">// 上午上课</span></span><br><span class="line">        work();</span><br><span class="line">        <span class="comment">// 中午吃饭</span></span><br><span class="line">        eat();</span><br><span class="line">        <span class="comment">// 中午午休</span></span><br><span class="line">        sleep();</span><br><span class="line">        <span class="comment">// 下午上课</span></span><br><span class="line">        work();</span><br><span class="line">        <span class="comment">// 下午吃饭</span></span><br><span class="line">        eat();</span><br><span class="line">        <span class="comment">// 晚上备课</span></span><br><span class="line">        preparation();</span><br><span class="line">        <span class="comment">// 晚上睡觉</span></span><br><span class="line">        sleep();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">eat</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;老师在吃饭&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">sleep</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;老师在睡觉&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">work</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;老师在上课&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">preparation</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;老师在备课&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String n)</span></span>&#123;</span><br><span class="line">        name = n;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getAge</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAge</span><span class="params">(<span class="keyword">int</span> a)</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(a&lt;<span class="number">26</span>||a&gt;<span class="number">65</span>)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;您输入的年龄不符合要求&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        age = a;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getTid</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> tid;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setTid</span><span class="params">(<span class="keyword">int</span> id)</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(id&lt;<span class="number">1000</span>||id&gt;<span class="number">9999</span>)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;你输入的id不符合要求&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        tid = id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getSex</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> sex;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setSex</span><span class="params">(String s)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 判断用户输入的性别是否合法</span></span><br><span class="line">        <span class="keyword">if</span>(s.equals(<span class="string">&quot;男&quot;</span>)||s.equals(<span class="string">&quot;女&quot;</span>))&#123;</span><br><span class="line">            sex = s;</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;您输入的性别不符合要求&quot;</span>);</span><br><span class="line">            <span class="comment">// 不符合要求就保密</span></span><br><span class="line">            sex = <span class="string">&quot;保密&quot;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="package-包名"><a href="#package-包名" class="headerlink" title="package 包名"></a>package 包名</h4><ul>
<li><p>在一个java项目中会有很多java类型，如果都放在一个包中非常难以查找。并且如果存在相同类名，回出现报错。此时应该使用不同的包对类进行分管。一般的包名规范：域名倒置+功能名</p>
<ul>
<li><p>例如：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">百度的域名</span><br><span class="line">map.baidu.com</span><br><span class="line">wenku.baidu.com</span><br><span class="line">image.baidu.com  </span><br><span class="line">tieba.baidu.com</span><br><span class="line">    </span><br><span class="line">百度的包名：（域名倒置）</span><br><span class="line">com.baidu.map</span><br><span class="line">com.baidu.wenku</span><br><span class="line">com.baidu.image</span><br><span class="line">com.baidu.tieba</span><br><span class="line">    </span><br><span class="line"></span><br><span class="line">包名创建如果不反写，会导致多个重复的名称的包存在，浪费资源</span><br><span class="line"></span><br><span class="line">使用域名作为包名也可以很好的分辨是哪一家公司的作品</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>
<h4 id="构造器-constructor"><a href="#构造器-constructor" class="headerlink" title="构造器 constructor"></a>构造器 constructor</h4><ul>
<li><p>类构造对象时调用的方法，主要用来实例化对象。构造方法分为无参构造、有参构造。构造方法是一种特殊的 “成员方法”</p>
<ul>
<li><p>构造方法名，必须与类名一致</p>
</li>
<li><p>构造方法如果不写默认会送一个无参构造</p>
</li>
<li><p>如果自己手动编写构造方法，就不会默认送无参构造</p>
</li>
<li><p>构造方法也支持重载，可以写多个，至少有一个</p>
</li>
<li><p>没有返回值类型，连 void 都不写（默认返回创建的实例对象）</p>
</li>
<li><p>每个实例的构造方法只会执行其中的一个，并且只执行一次</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// 默认的无参构造</span></span><br><span class="line"><span class="keyword">public</span> 类名()&#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>创建对象的过程：</p>
<ul>
<li>在堆中分配对象的存储空间</li>
<li>对实例对象的所有的成员变量进行默认赋值</li>
<li>执行构造方法，返回实例对象</li>
</ul>
</li>
<li><p><strong>注意！一个类中如果手动创建了有参，那么必须再创建一个无参的构造，否则无参构造不能使用！</strong></p>
</li>
<li><p>代码展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Dog</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Dog 类的无参构造，如果不写其他构造则会默认送一个</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"><span class="comment">//    public Dog()&#123;</span></span><br><span class="line"><span class="comment">//        System.out.println(&quot;调用 Dog 的无参构造创建实例&quot;);</span></span><br><span class="line"><span class="comment">//    &#125;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Dog 类的有参构造，一般有参构造用于给成员变量赋初始值</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> n 狗的姓名</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Dog</span><span class="params">(String n)</span></span>&#123;</span><br><span class="line">        name = n;</span><br><span class="line">        System.out.println(<span class="string">&quot;调用 Dog 的有参构造创建实例&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Dog</span><span class="params">(<span class="keyword">int</span> n)</span></span>&#123;</span><br><span class="line">        System.out.println(n);</span><br><span class="line">        System.out.println(<span class="string">&quot;调用 Dog 的有参构造创建实例&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String n)</span></span>&#123;</span><br><span class="line">        name = n;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="this-关键字"><a href="#this-关键字" class="headerlink" title="this 关键字"></a>this 关键字</h4><ul>
<li><p>每一个非静态的方法都会有一个默认的局部变量叫做 this，它永远指向调用它的当前实例。简单来讲，那个实例调用的方法，this就是那个实例</p>
</li>
<li><p>this的使用</p>
<ul>
<li>this.属性名（成员变量名）<ul>
<li>调用当前实例的成员变量</li>
</ul>
</li>
<li>this.方法名<ul>
<li>调用当前实例的成员方法</li>
</ul>
</li>
<li>this()<ul>
<li>调用当前类的无参构造</li>
<li>必须写在构造方法中，并且只能写在第一句</li>
</ul>
</li>
<li>this(参数)<ul>
<li>调用当前类的有参构造</li>
<li>必须写在构造方法中，并且只能写在第一句</li>
</ul>
</li>
<li>注意！谁调用方法谁就是 this，因为 this 只能写在方法中</li>
</ul>
</li>
<li><p>代码展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Dog</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Dog 类的无参构造，如果不写其他构造则会默认送一个</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Dog</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">// this() 必须写在构造方法中，并且必须为第一行！</span></span><br><span class="line"><span class="comment">//        this.name = &quot;张三&quot;;</span></span><br><span class="line">        <span class="comment">// 使用 this 关键字对有参构造进行调用</span></span><br><span class="line">        <span class="keyword">this</span>(<span class="string">&quot;旺财&quot;</span>);</span><br><span class="line"><span class="comment">//        this.name = &quot;旺财&quot;;</span></span><br><span class="line">        System.out.println(<span class="string">&quot;调用 Dog 的无参构造创建实例&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Dog 类的有参构造，一般有参构造用于给成员变量赋初始值</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> name 狗的姓名</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Dog</span><span class="params">(String name)</span></span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">        System.out.println(<span class="string">&quot;调用 Dog 的有参构造创建实例&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Dog</span><span class="params">(<span class="keyword">int</span> n)</span></span>&#123;</span><br><span class="line">        System.out.println(n);</span><br><span class="line">        System.out.println(<span class="string">&quot;调用 Dog 的有参构造创建实例&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">// 使用 this 关键字可以访问当前实例的成员变量或方法</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span></span>&#123;</span><br><span class="line">        <span class="comment">// this() 只能在构造方法中使用！</span></span><br><span class="line"><span class="comment">//        this();</span></span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="String-的使用"><a href="#String-的使用" class="headerlink" title="String 的使用"></a>String 的使用</h4><ul>
<li><p>字符串是引用类型，用于存储多个字符，只要使用双引号包裹的就是字符串</p>
</li>
<li><p>String 的特点：</p>
<ul>
<li>字符串内容永远都不会改变</li>
<li>JDK 1.8 (java8) 中字符串的底层是使用 char[] 字符数组实现</li>
<li>JDK 1.9 (java9) 底层实现原理是 byte[] 字节数组 (二进制存储)</li>
<li>String 字符串可以被共享使用 (常量池)</li>
</ul>
</li>
<li><p>在使用引用类型时候需要通过 new 关键字创建实例，而创建 String 对象可以省略</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// 使用 new 关键字进行实例创建</span></span><br><span class="line">String name = <span class="keyword">new</span> String(<span class="string">&quot;abc&quot;</span>);</span><br><span class="line"><span class="comment">// java 定义了 String 的语法糖，简化创建 String 对象</span></span><br><span class="line">String name = <span class="string">&quot;abc&quot;</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>String 的源码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// String 类被 final 修饰表示不可以被继承</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">final</span> <span class="class"><span class="keyword">class</span> <span class="title">String</span></span></span><br><span class="line"><span class="class">    <span class="keyword">implements</span> <span class="title">java</span>.<span class="title">io</span>.<span class="title">Serializable</span>, <span class="title">Comparable</span>&lt;<span class="title">String</span>&gt;, <span class="title">CharSequence</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 在 String 中有一个被 final 修饰的字符数组成员变量</span></span><br><span class="line">    <span class="comment">// 表示为一经赋值后不可修改值得 char 数组</span></span><br><span class="line">    <span class="comment">/** The value is used for character storage. */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">char</span> value[];</span><br><span class="line">    </span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>String 中的构造函数</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 无参构造：new String();</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">String</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 传入一个字符串创建：new String(&quot;abc&quot;);</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">String</span><span class="params">(String original)</span> </span>&#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 使用 char 数组创建：new String(new char[]&#123;&#x27;a&#x27;,&#x27;b&#x27;,&#x27;c&#x27;&#125;);</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">String</span><span class="params">(<span class="keyword">char</span> value[])</span> </span>&#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 使用 char 数组指定其中的某一段创建：new String(new char[]&#123;&#x27;a&#x27;,&#x27;b&#x27;,&#x27;c&#x27;,&#x27;d&#x27;,&#x27;e&#x27;,&#x27;f&#x27;&#125;,2,2);</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">String</span><span class="params">(<span class="keyword">char</span> value[], <span class="keyword">int</span> offset, <span class="keyword">int</span> count)</span> </span>&#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 使用 int 数组配合 unicode 码指定其中一段创建：new String(new int[]&#123;97,98,99,100,101,102&#125;,1,2);</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">String</span><span class="params">(<span class="keyword">int</span>[] codePoints, <span class="keyword">int</span> offset, <span class="keyword">int</span> count)</span> </span>&#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h5 id="String-中的常用方法"><a href="#String-中的常用方法" class="headerlink" title="String 中的常用方法"></a>String 中的常用方法</h5><ul>
<li><p>String 本身就是一个别人写好的字符串类，其中有很多属性和方法。那么这些方法都应该是与字符串有关的操作</p>
</li>
<li><p>String 类中的方法有很多，死记硬背是背不完的，也没有必要。更多的就是我们自己去使用，或者查 API ，看源码，百度</p>
</li>
<li><p>代码展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StringFunction</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 字符串常用的实例化方式</span></span><br><span class="line">        String str1 = <span class="string">&quot;&quot;</span>;</span><br><span class="line">        String str2 = <span class="keyword">new</span> String();</span><br><span class="line">        String str3 = <span class="keyword">new</span> String(<span class="string">&quot;abc&quot;</span>);</span><br><span class="line">        String str4 = <span class="keyword">new</span> String(<span class="keyword">new</span> <span class="keyword">char</span>[]&#123;<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>&#125;);</span><br><span class="line">        String str5 = <span class="keyword">new</span> String</span><br><span class="line">                (<span class="keyword">new</span> <span class="keyword">char</span>[]&#123;<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>,<span class="string">&#x27;e&#x27;</span>,<span class="string">&#x27;f&#x27;</span>&#125;,<span class="number">2</span>,<span class="number">2</span>);</span><br><span class="line">        String str6 = <span class="keyword">new</span> String</span><br><span class="line">                (<span class="keyword">new</span> <span class="keyword">int</span>[]&#123;<span class="number">97</span>,<span class="number">98</span>,<span class="number">99</span>,<span class="number">100</span>,<span class="number">101</span>,<span class="number">102</span>&#125;,<span class="number">2</span>,<span class="number">2</span>);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;str5 = &quot;</span> + str5);</span><br><span class="line">        System.out.println(<span class="string">&quot;str6 = &quot;</span> + str6);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;======================================&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 创建一个字符串</span></span><br><span class="line">        String str = <span class="string">&quot;abcdefabc&quot;</span>;</span><br><span class="line">        System.out.println(<span class="string">&quot;str = &quot;</span> + str);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// length() 获取字符串长度</span></span><br><span class="line">        System.out.println(<span class="string">&quot;str.length() = &quot;</span> + str.length());</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">// charAt(index) 返回指定位置的字符，如果超出会报错</span></span><br><span class="line">        System.out.println(<span class="string">&quot;str.charAt(4) = &quot;</span> + str.charAt(<span class="number">4</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// indexOf(字符) 返回指定字符首次出现的位置，如果没找到返回 -1</span></span><br><span class="line">        System.out.println(<span class="string">&quot;str.indexOf(&#x27;c&#x27;) = &quot;</span> + str.indexOf(<span class="string">&#x27;c&#x27;</span>));</span><br><span class="line">        System.out.println(<span class="string">&quot;str.indexOf(101) = &quot;</span> + str.indexOf(<span class="number">101</span>));</span><br><span class="line">        System.out.println(<span class="string">&quot;str.indexOf(\&quot;de\&quot;) = &quot;</span> + str.indexOf(<span class="string">&quot;de&quot;</span>));</span><br><span class="line">        <span class="comment">// indexOf(字符,start) 也可以指定从哪一位开始查找</span></span><br><span class="line">        System.out.println(<span class="string">&quot;str.indexOf(\&quot;bc\&quot;,5) = &quot;</span> + str.indexOf(<span class="string">&quot;bc&quot;</span>, <span class="number">5</span>));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">// lastIndexOf(字符) 返回指定字符最后出现的位置，如果没找到返回 -1</span></span><br><span class="line">        System.out.println(<span class="string">&quot;str.lastIndexOf(\&quot;bc\&quot;) = &quot;</span> + str.lastIndexOf(<span class="string">&quot;bc&quot;</span>));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">// substring([下标1,下标2)/[下标1]) 截取字符串返回 1-2不包括2/ 返回下标1以后内容</span></span><br><span class="line">        System.out.println(<span class="string">&quot;str.substring(2) = &quot;</span> + str.substring(<span class="number">2</span>));</span><br><span class="line">        System.out.println(<span class="string">&quot;str.substring(2,5) = &quot;</span> + str.substring(<span class="number">2</span>, <span class="number">5</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// split(str) 将字符串通过指定字符进行分割，返回字符串数组</span></span><br><span class="line">        String[] split = str.split(<span class="string">&quot;&quot;</span>);</span><br><span class="line">        System.out.println(Arrays.toString(split));</span><br><span class="line">        String[] split1 = str.split(<span class="string">&quot;b&quot;</span>);</span><br><span class="line">        System.out.println(Arrays.toString(split1));</span><br><span class="line">        <span class="comment">// split(str,limit) 可以通过 limit 设置最终数组长度</span></span><br><span class="line">        String[] split2 = str.split(<span class="string">&quot;b&quot;</span>,<span class="number">2</span>);</span><br><span class="line">        System.out.println(Arrays.toString(split2));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">// toUpperCase() 转换为大写</span></span><br><span class="line">        System.out.println(<span class="string">&quot;str.toUpperCase() = &quot;</span> + str.toUpperCase());</span><br><span class="line">        <span class="comment">// 转换为小写</span></span><br><span class="line">        System.out.println(<span class="string">&quot;str.toLowerCase() = &quot;</span> + str.toLowerCase());</span><br><span class="line"></span><br><span class="line">        <span class="comment">// equalsIgnoreCase(str) 忽略大小写进行比较</span></span><br><span class="line">        System.out.println(<span class="string">&quot;aBcDefabc&quot;</span>.equalsIgnoreCase(str));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">// trim() 去除字符串的前后空格</span></span><br><span class="line">        str = <span class="string">&quot;   qwe  qwe     &quot;</span>;</span><br><span class="line">        System.out.println(<span class="string">&quot;str.length() = &quot;</span> + str.length());</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;str.trim().length() = &quot;</span> + str.trim().length());</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h5 id="制作-String-的自定义工具类"><a href="#制作-String-的自定义工具类" class="headerlink" title="制作 String 的自定义工具类"></a>制作 String 的自定义工具类</h5><ul>
<li><p>虽然 String 提供了很多操作字符串的方法，但是往往需求多于方法。我们就需要对应不同的需求提供不同的解决方法。这时候就需要我们定制自己的专属 String 工具类</p>
</li>
<li><p>需求：</p>
<ul>
<li>可以统计字符的出现次数<ul>
<li>单个字符统计</li>
<li>单词统计</li>
<li>任意字符统计</li>
</ul>
</li>
<li>可以随机生成字符<ul>
<li>随机生成小写字符</li>
<li>随机生成大写字符</li>
<li>随机生成大小写混合字符</li>
</ul>
</li>
</ul>
</li>
<li><p>代码展示：StringUtil</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StringUtil</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 在指定的字符串中统计指定字符出现的次数</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> str 指定的字符串</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> c 需要查找的字符</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 字符出现次数</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">countForChar</span><span class="params">(String str,<span class="keyword">char</span> c)</span></span>&#123;</span><br><span class="line">        <span class="keyword">int</span> count = <span class="number">0</span>;</span><br><span class="line"><span class="comment">//        for (int i = 0; i &lt; str.length(); i++) &#123;</span></span><br><span class="line"><span class="comment">//            if (str.charAt(i)==c)&#123;</span></span><br><span class="line"><span class="comment">//                count++;</span></span><br><span class="line"><span class="comment">//            &#125;</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 逻辑替换</span></span><br><span class="line"><span class="comment">//        int index = -1;</span></span><br><span class="line"><span class="comment">//        while ((index = str.indexOf(c,index+1))!=-1)&#123;</span></span><br><span class="line"><span class="comment">//            // 如果找到赋予查找的下标</span></span><br><span class="line"><span class="comment">////            index = str.indexOf(find,index+1);</span></span><br><span class="line"><span class="comment">//            count++;</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 重载替换</span></span><br><span class="line">        count = countAnyString(str, c+<span class="string">&quot;&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> count;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 在指定的字符串中统计指定单词出现的次数</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> str 指定的字符串</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> word 需要统计的单词</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 单词出现次数</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">countWord</span><span class="params">(String str,String word)</span></span>&#123;</span><br><span class="line">        <span class="comment">// str = &quot;hello word i am very happy come here hello happy hellohello&quot;</span></span><br><span class="line">        <span class="keyword">int</span> count = <span class="number">0</span>;</span><br><span class="line">        <span class="comment">// 指定空格分割字符串</span></span><br><span class="line">        String[] arr = str.split(<span class="string">&quot; &quot;</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="keyword">int</span> index = -<span class="number">1</span>;</span><br><span class="line">            index = arr[i].indexOf(word);</span><br><span class="line">            <span class="comment">// 先判断是否出现，再通过长度确认保持一致</span></span><br><span class="line">            <span class="keyword">if</span>(index &gt; -<span class="number">1</span> &amp;&amp; word.length() == arr[i].length())&#123;</span><br><span class="line">                count++;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> count;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 在指定的字符串中统计指定任意字符串出现的次数</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> str 指定的字符串</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> find 需要查找的任意字符串</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 字符串出现次数</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">countAnyString</span><span class="params">(String str,String find)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 记录重复次数</span></span><br><span class="line">        <span class="keyword">int</span> count = <span class="number">0</span>;</span><br><span class="line">        <span class="comment">// 记录查找的位置</span></span><br><span class="line">        <span class="keyword">int</span> index = -<span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//        do&#123;</span></span><br><span class="line"><span class="comment">//            // 如果找到赋予查找的下标</span></span><br><span class="line"><span class="comment">//            index = str.indexOf(find,index+1);</span></span><br><span class="line"><span class="comment">//            if(index!=-1)&#123;</span></span><br><span class="line"><span class="comment">//                count++;</span></span><br><span class="line"><span class="comment">//            &#125;</span></span><br><span class="line"><span class="comment">//        &#125;while (index!=-1);</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span> ((index = str.indexOf(find,index+<span class="number">1</span>))!=-<span class="number">1</span>)&#123;</span><br><span class="line">            <span class="comment">// 如果找到赋予查找的下标</span></span><br><span class="line"><span class="comment">//            index = str.indexOf(find,index+1);</span></span><br><span class="line">            count++;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> count;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 随机生成任意长度的全小写字符串(a-z)</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> length 指定生成字符串的长度</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 随机生成的字符串</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">randomLowerString</span><span class="params">(<span class="keyword">int</span> length)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 创建一个 char 数组，存储生成的字符</span></span><br><span class="line">        <span class="keyword">int</span>[] arr = <span class="keyword">new</span> <span class="keyword">int</span>[length];</span><br><span class="line">        <span class="comment">// 通过循环随机生成字符串</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="comment">// a-z 阿斯克码范围 [97,123)</span></span><br><span class="line">            arr[i] = (<span class="keyword">int</span>)(Math.random()*<span class="number">26</span>+<span class="number">97</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> String(arr,<span class="number">0</span>,arr.length);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 随机生成任意长度的全大写字符串(A-Z)</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> length 指定生成字符串的长度</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 随机生成的字符串</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">randomUpperString</span><span class="params">(<span class="keyword">int</span> length)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 创建一个 char 数组，存储生成的字符</span></span><br><span class="line">        <span class="keyword">int</span>[] arr = <span class="keyword">new</span> <span class="keyword">int</span>[length];</span><br><span class="line">        <span class="comment">// 通过循环随机生成字符串</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="comment">// A-Z 阿斯克码范围 [65,91)</span></span><br><span class="line">            arr[i] = (<span class="keyword">int</span>)(Math.random()*<span class="number">26</span>+<span class="number">65</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> String(arr,<span class="number">0</span>,arr.length);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 随机生成任意长度的大小写随机字符串(a-z，A-Z)</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> length 指定生成字符串的长度</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 随机生成的字符串</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">randomString</span><span class="params">(<span class="keyword">int</span> length)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 创建一个 char 数组，存储生成的字符</span></span><br><span class="line">        <span class="keyword">int</span>[] arr = <span class="keyword">new</span> <span class="keyword">int</span>[length];</span><br><span class="line">        <span class="comment">// 通过循环随机生成字符串</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> ((<span class="keyword">int</span>)(Math.random()*<span class="number">2</span>)==<span class="number">0</span>)&#123;</span><br><span class="line">                <span class="comment">// a-z 阿斯克码范围 [97,123)</span></span><br><span class="line">                arr[i] = (<span class="keyword">int</span>)(Math.random()*<span class="number">26</span>+<span class="number">97</span>);</span><br><span class="line">                <span class="keyword">continue</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// A-Z 阿斯克码范围 [65,91)</span></span><br><span class="line">            arr[i] = (<span class="keyword">int</span>)(Math.random()*<span class="number">26</span>+<span class="number">65</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> String(arr,<span class="number">0</span>,arr.length);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h5 id="拓展-String-的对象创建存储问题"><a href="#拓展-String-的对象创建存储问题" class="headerlink" title="拓展 String 的对象创建存储问题"></a>拓展 String 的对象创建存储问题</h5><ul>
<li><p>因为 String 的值不可被修改的，所以 String 的值都会在创建时放入一个容器中进行统一存储。当再次需要使用时，会在该容器中获取，这个容器就是常量池</p>
<ul>
<li>注意！只要使用了 new 关键字的都会在堆中开辟一个新的空间</li>
</ul>
</li>
<li><p>代码展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test01</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 创建字符串</span></span><br><span class="line">        <span class="comment">// 此时没有使用 new 关键字，是因为语法糖的原因，本质上依然会在堆中开辟空间</span></span><br><span class="line">        <span class="comment">// &quot;hello&quot; 回在字符串常量池中存储一个引用地址</span></span><br><span class="line">        <span class="comment">// 它本身会和其他实例对象一样存储在堆中</span></span><br><span class="line">        String str1 = <span class="string">&quot;hello&quot;</span>;</span><br><span class="line">        <span class="comment">// 使用语法糖再次创建相同内容，依然指向同一个堆中的地址</span></span><br><span class="line">        String str2 = <span class="string">&quot;hello&quot;</span>;</span><br><span class="line">        <span class="comment">// 记住！都使用语法糖格式创建相同内容的字符串，地址值一定相同</span></span><br><span class="line">        System.out.println(<span class="string">&quot;(str1 == str2) = &quot;</span> + (str1 == str2));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 只要使用了 new 关键字，就会在堆中开辟一个新空间</span></span><br><span class="line">        String str3 = <span class="keyword">new</span> String(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">        <span class="comment">// 此时无论跟谁比较都是 false</span></span><br><span class="line">        System.out.println(<span class="string">&quot;(str2 == str3) = &quot;</span> + (str2 == str3));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 使用 + 号拼接常量，虚拟机会帮我们优化为一个完整的常量</span></span><br><span class="line">        String str4 = <span class="string">&quot;h&quot;</span>+<span class="string">&quot;e&quot;</span>+<span class="string">&quot;l&quot;</span>+<span class="string">&quot;l&quot;</span>+<span class="string">&quot;o&quot;</span>;</span><br><span class="line">        System.out.println(<span class="string">&quot;(str2 == str4) = &quot;</span> + (str2 == str4));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 使用 + 号拼接变量，虚拟机不会帮我们优化</span></span><br><span class="line">        String hel = <span class="string">&quot;hel&quot;</span>;</span><br><span class="line">        String str5 = hel + <span class="string">&quot;l&quot;</span> + <span class="string">&quot;o&quot;</span>;</span><br><span class="line">        System.out.println(<span class="string">&quot;(str2 == str5) = &quot;</span> + (str2 == str5));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 使用语法糖创建相同内容的字符串地址一定相同</span></span><br><span class="line">        <span class="comment">// 使用 new 关键创建的字符串地址值一定不相同</span></span><br><span class="line">        <span class="comment">// 常量拼接会优化，变量拼接是未知</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="自动拆装箱"><a href="#自动拆装箱" class="headerlink" title="自动拆装箱"></a>自动拆装箱</h4><ul>
<li><p>基本数据类型，是用起来非常方便，但是没有对应的方法来操作这些基本类型的数据。可以思考下，如果有一个类，把基本数据类型的数据封装起来，提供一些相关的操作方法，这种类就叫做 包装类(wrapper)</p>
</li>
<li><p>在 java 中对应的八大基本类型各自都有一个包装类，他们让基本数据类型变量具有类中的对象的特征。可以使用方法，可以存储 null 值</p>
</li>
<li><p>八大基本数据类型对应的包装类：</p>
<table>
<thead>
<tr>
<th align="center">基本数据类型</th>
<th align="center">包装类</th>
</tr>
</thead>
<tbody><tr>
<td align="center">byte</td>
<td align="center">Byte</td>
</tr>
<tr>
<td align="center">short</td>
<td align="center">Short</td>
</tr>
<tr>
<td align="center"><strong>int</strong></td>
<td align="center"><strong>Integer</strong></td>
</tr>
<tr>
<td align="center">long</td>
<td align="center">Long</td>
</tr>
<tr>
<td align="center">float</td>
<td align="center">Float</td>
</tr>
<tr>
<td align="center">double</td>
<td align="center">Double</td>
</tr>
<tr>
<td align="center"><strong>char</strong></td>
<td align="center"><strong>Character</strong></td>
</tr>
<tr>
<td align="center">boolean</td>
<td align="center">Boolean</td>
</tr>
</tbody></table>
</li>
</ul>
<h5 id="拆箱和装箱展示"><a href="#拆箱和装箱展示" class="headerlink" title="拆箱和装箱展示"></a>拆箱和装箱展示</h5><ul>
<li><p>装箱：对基本类型进行包装，基本类型 ==&gt; 包装类</p>
</li>
<li><p>拆箱：对包装类型进行拆包，包装类 ==&gt; 基本类型</p>
</li>
<li><p>代码展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BoxingAndUnboxing</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 装箱</span></span><br><span class="line">        <span class="comment">// 通过包装类的构造器实现(jdk1.5之前)</span></span><br><span class="line">        Integer num = <span class="keyword">new</span> Integer(<span class="number">123</span>);</span><br><span class="line">        Integer num2 = <span class="keyword">new</span> Integer(<span class="string">&quot;123&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;num = &quot;</span> + num);</span><br><span class="line">        <span class="comment">// 自动装箱(jdk.15后)</span></span><br><span class="line">        <span class="comment">// 相当于编译器自动做以下的语法编译</span></span><br><span class="line">        <span class="comment">// Integer i = Integer.valueOf(100);</span></span><br><span class="line">        Integer i = <span class="number">100</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 拆箱</span></span><br><span class="line">        <span class="comment">// 调用包装类中的 .xxxValue() 方法</span></span><br><span class="line">        <span class="comment">// num 是一个装箱数据</span></span><br><span class="line">        <span class="keyword">int</span> num3 = num.intValue();</span><br><span class="line">        <span class="comment">// 自动拆箱</span></span><br><span class="line">        <span class="comment">// 相当于编译器自动做以下的语法编译</span></span><br><span class="line">        <span class="comment">// int num4 = num.inValue();</span></span><br><span class="line">        <span class="keyword">int</span> num4 = num;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 题目一：</span></span><br><span class="line">        Integer i1 = <span class="number">76</span>;</span><br><span class="line">        <span class="keyword">int</span> i2 = <span class="number">76</span>;</span><br><span class="line">        <span class="comment">// 此时答案是 true ，但是为什么呢？</span></span><br><span class="line">        <span class="comment">// 明明这里使用的是一个引用类型和一个基本类型比较</span></span><br><span class="line">        System.out.println(<span class="string">&quot;(i1 == i2) = &quot;</span> + (i1 == i2));<span class="comment">// true</span></span><br><span class="line">        <span class="comment">// 如果包装类与基本类型进行运算比较时，会自动进行拆箱操作</span></span><br><span class="line">        <span class="comment">// 最终的比较其实就是 int == int 进行比较</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 题目二：自动拆装箱缓存机制</span></span><br><span class="line">        Integer i3 = <span class="number">80</span>;</span><br><span class="line">        Integer i4 = <span class="number">80</span>;</span><br><span class="line">        System.out.println(<span class="string">&quot;(i3 == i4) = &quot;</span> + (i3 == i4));<span class="comment">// true</span></span><br><span class="line">        <span class="comment">// 如果创建了一个 Integer 对象，会随之创建一个 IntegerCache 缓存</span></span><br><span class="line">        <span class="comment">// 其中存放了最基础的 byte 范围数值 [-128,127]</span></span><br><span class="line">        <span class="comment">// 如果创建的 Integer 对象的范围在缓存之内，则不会创建新的对象</span></span><br><span class="line"></span><br><span class="line">        </span><br><span class="line"></span><br><span class="line">        Integer i5 = <span class="number">280</span>;</span><br><span class="line">        Integer i6 = <span class="number">280</span>;</span><br><span class="line">        System.out.println(<span class="string">&quot;(i5 == i6) = &quot;</span> + (i5 == i6));<span class="comment">// false</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        Integer i7 = <span class="keyword">new</span> Integer(<span class="number">50</span>);</span><br><span class="line">        Integer i8 = <span class="number">50</span>;</span><br><span class="line">        <span class="comment">// 使用 new 关键字创建对象，指向新的实例地址</span></span><br><span class="line">        System.out.println(<span class="string">&quot;(i7 == i8) = &quot;</span> + (i7 == i8));</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>总结：</p>
<ul>
<li>装箱：基本类型 ==&gt; 包装类，包装类.valueOf(值)</li>
<li>拆箱：包装类 ==&gt; 基本类型，包装值.xxxValue()</li>
<li>自动拆装箱缓存机制，整型在 [-128,127] 之间的值会有缓存，使用语法糖创建在此范围内的值，都会从缓存中获取</li>
</ul>
</li>
</ul>
<h4 id="封装练习"><a href="#封装练习" class="headerlink" title="封装练习"></a>封装练习</h4><h5 id="封装超级数组"><a href="#封装超级数组" class="headerlink" title="封装超级数组"></a>封装超级数组</h5><ul>
<li><p>之前使用数进行多个数据的存储，可是数组虽然可以存储多个数据，但是创建后长度不可变。既然学会了封装，也可以尝试封装一个自己的自制数组。称之为：超级数组，SupperArray</p>
</li>
<li><p>代码展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SupperArray</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** 定义一个数组存放数据 */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span>[] values;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** 定义一个真实大小属性 */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> size;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 无参构造，初始化数组</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">SupperArray</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 给数组设置一个默认的初始值</span></span><br><span class="line">        <span class="keyword">this</span>(<span class="number">8</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 使用有参构造自定义数组的初始化大小</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> initialCapacity 数组的初始化容量</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">SupperArray</span><span class="params">(<span class="keyword">int</span> initialCapacity)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 根据传入的数值进行数组的初始化</span></span><br><span class="line">        <span class="keyword">this</span>.values = <span class="keyword">new</span> <span class="keyword">int</span>[initialCapacity];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// ============================================</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 超级数组扩容的方法</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> minCapacity 最小容量</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">grow</span><span class="params">(<span class="keyword">int</span> minCapacity)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 将数组扩容</span></span><br><span class="line">        <span class="keyword">int</span> oldCapacity = values.length;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 将数组扩容为原有的 1.5 倍</span></span><br><span class="line">        <span class="keyword">int</span> newCapacity = oldCapacity + oldCapacity/<span class="number">2</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 如果出现 1 和 0 就需要使用最小值扩容</span></span><br><span class="line">        <span class="keyword">if</span> (minCapacity - newCapacity&gt;<span class="number">0</span>)&#123;</span><br><span class="line">            newCapacity = minCapacity;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 创建新的扩容后数组</span></span><br><span class="line">        <span class="keyword">int</span>[] temp = <span class="keyword">new</span> <span class="keyword">int</span>[newCapacity];</span><br><span class="line">        <span class="comment">// 通过循环将原有数据放入新的数组中</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; values.length; i++) &#123;</span><br><span class="line">            temp[i] = values[i];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 将新的数组赋值给原数组</span></span><br><span class="line">        values = temp;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 添加数据的方法，末尾追加，尾插法</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> data 需要添加的数据</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 返回添加成功与否</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> data)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 判断元素添加后个数是否超过数组长度</span></span><br><span class="line">        <span class="keyword">if</span> (size + <span class="number">1</span> &gt; values.length)&#123;</span><br><span class="line">            <span class="comment">// 调用扩容方法</span></span><br><span class="line">            grow(values.length+<span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 向数组中插入需要添加的数据，并且当前下标后移</span></span><br><span class="line">        values[size++] = data;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 根据下标删除指定元素</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> index 删除元素的下标</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 返回删除成功与否</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">remove</span><span class="params">(<span class="keyword">int</span> index)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 如果传入的下标超出数组范围则删除失败</span></span><br><span class="line">        <span class="keyword">if</span>(index&lt;<span class="number">0</span> || index &gt;= size)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;您需要删除的下标&quot;</span> +</span><br><span class="line">                    <span class="string">&quot;[&quot;</span>+index+<span class="string">&quot;]不在范围内&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 如果下标合法则进行删除</span></span><br><span class="line">        <span class="comment">// &#123;1, 2, 3, 4, 5, 6&#125; =》3=》 &#123;1, 2, 3, 5, 6, 6&#125;</span></span><br><span class="line">        <span class="comment">// 删除就是将数组中被删除的元素的右侧元素值向左移动</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = index + <span class="number">1</span>; i &lt; size; i++) &#123;</span><br><span class="line">            values[i-<span class="number">1</span>] = values[i];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 如果想设置为 0</span></span><br><span class="line">        <span class="comment">// &#123;1, 2, 3, 5, 6, 0&#125;</span></span><br><span class="line">        values[--size] = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 根据下标查询元素</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> index 查询元素的下标</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 返回查询的数据</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">get</span><span class="params">(<span class="keyword">int</span> index)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 判断查询元素的下标是否超出数组的真实存储范围</span></span><br><span class="line">        <span class="keyword">if</span>(index &lt; <span class="number">0</span> || index &gt;= size)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;您需要查找的下标&quot;</span> +</span><br><span class="line">                    <span class="string">&quot;[&quot;</span>+index+<span class="string">&quot;]不在范围内&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 如果每超出范围则直接返回</span></span><br><span class="line">        <span class="keyword">return</span> values[index];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 根据指定下标修改元素</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> index 修改元素的下标</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> data 修改后的数据</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 返回修改成功与否</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">set</span><span class="params">(<span class="keyword">int</span> index,<span class="keyword">int</span> data)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 判断修改元素的下标是否超出数组的真实存储范围</span></span><br><span class="line">        <span class="keyword">if</span>(index &lt; <span class="number">0</span> || index &gt;= size)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;您需要修改的下标&quot;</span> +</span><br><span class="line">                    <span class="string">&quot;[&quot;</span>+index+<span class="string">&quot;]不在范围内&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 如果下标没有超出范围，直接修改</span></span><br><span class="line">        values[index] = data;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">// ============================================</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取当前存储的真实数据个数</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 返回存储的元素个数</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> size;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 定义打印方法，打印数据数组中的内容</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 打印的字符串</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span></span>&#123;</span><br><span class="line">        String str = <span class="string">&quot;[&quot;</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; size; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span>(i == size-<span class="number">1</span>)&#123;</span><br><span class="line">                str += values[i];</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            str += values[i] + <span class="string">&quot;, &quot;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        str += <span class="string">&quot;]&quot;</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> str;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="面向对象之继承"><a href="#面向对象之继承" class="headerlink" title="面向对象之继承"></a>面向对象之继承</h3><h4 id="继承的概述"><a href="#继承的概述" class="headerlink" title="继承的概述"></a>继承的概述</h4><ul>
<li>继承就是可以解决代码的复用问题，一个类可以继承另一个类，被继承的类我们称之为 父类 或者 超类、基类，另一个类称之为子类 也叫 派生类 。子类可以通过 extends 关键字轻松的拥有父类的成员变量和成员方法的能力，除了被 private 修饰的(也不能被 final 修饰)。在 java 中式单继承的，这样可以使代码规范的实现</li>
<li>如何抽离父类：将共同的特征(属性和方法)提取出来。写在一个类中，其他的类只需要继承这个类，那么就会拥有这个类的共同特征。<ul>
<li>提高代码的复用性</li>
<li>只支持单继承，一个子类只有一个直接父类</li>
<li>每个类都会继承一个直接父类，如果没有写继承，会默认添加 extends Object</li>
</ul>
</li>
<li>父类的优点在于给其子类提供模板，缺点在于只能单继承，并且约束性不高</li>
</ul>
<h4 id="继承简单示例"><a href="#继承简单示例" class="headerlink" title="继承简单示例"></a>继承简单示例</h4><ul>
<li><p>要求：分别创建三个类 Father、Son、GrandSon 三个类</p>
<ul>
<li><p>Father：有姓名，吃东西的方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Father</span> </span>&#123;</span><br><span class="line">    <span class="keyword">public</span> String name = <span class="string">&quot;卡卡罗特&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">eat</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;退休生活，吃好喝好&quot;</span>);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>Son：有姓名，工作，吃东西的方法，工作的方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Son</span> <span class="keyword">extends</span> <span class="title">Father</span></span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String job = <span class="string">&quot;水果忍者&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">work</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;奋斗年华，努力工作&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>GrandSon：有姓名，工作，梦想，吃东西的方法，工作的方法，玩的方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GrandSon</span> <span class="keyword">extends</span> <span class="title">Son</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String dream = <span class="string">&quot;神龙斗士&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">play</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;从早玩到晚&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>
<h4 id="子父类的构造执行顺序"><a href="#子父类的构造执行顺序" class="headerlink" title="子父类的构造执行顺序"></a>子父类的构造执行顺序</h4><ul>
<li><p>如果创建一个子类实例，父类会被创建吗？如果会创建，那么到底是谁的构造方法先执行？</p>
</li>
<li><p>给之前的创建的三个类都添加无参构造</p>
<ul>
<li><p>给 Father 添加</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">Father</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;Father 的无参构造被执行&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>给 Son 添加</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">Son</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;Son 的无参构造被执行&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>给 GrandSon 添加</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">GrandSon</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;GrandSon 的无参构造被执行&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>执行代码测试结果</p>
<ul>
<li>执行顺序：优先创建父类实例，逐级向下实例化</li>
</ul>
</li>
</ul>
</li>
</ul>
<h4 id="super-关键字"><a href="#super-关键字" class="headerlink" title="super 关键字"></a>super 关键字</h4><ul>
<li><p>在之前的课程中，我们可以使用 this 关键字指向当前实例，同样的类中还可以使用一个 super 关键字，它指向了父类的实例对象。super的使用与 this 类似</p>
</li>
<li><p>super 的使用</p>
<ul>
<li>super.属性名（成员变量名）<ul>
<li>访问父类实例的成员变量，可以写在子类的任意非静态中</li>
</ul>
</li>
<li>super.方法名<ul>
<li>访问父类实例的成员方法，可以写在子类的任意非静态中</li>
</ul>
</li>
<li>super()<ul>
<li>调用父类的无参构造</li>
<li>必须写在构造方法中，并且只能写在第一句</li>
</ul>
</li>
<li>super(参数)<ul>
<li>调用父类的有参构造</li>
<li>必须写在构造方法中，并且只能写在第一句</li>
</ul>
</li>
<li>注意！在子类的构造中第一行默认都会隐式添加一个 super()，最终总是会执行父类构造</li>
</ul>
</li>
</ul>
<h5 id="使用-super-访问父类属性和方法"><a href="#使用-super-访问父类属性和方法" class="headerlink" title="使用 super 访问父类属性和方法"></a>使用 super 访问父类属性和方法</h5><ul>
<li><p>在子类中调用父类的非私有属性和方法时，大致过程如下</p>
<ol>
<li>先在直接父类中查找</li>
<li>如果直接父类没有，则继续向上查找</li>
<li>最终达到一个所有类的共同父类，就是 Object</li>
</ol>
</li>
<li><p>代码展示</p>
<ul>
<li><p>修改 GrandSon</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">play</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="comment">// 通过 super 调用父类实例的方法</span></span><br><span class="line">    <span class="keyword">super</span>.eat();</span><br><span class="line">    <span class="comment">// 通过 super 访问父类的属性</span></span><br><span class="line">    System.out.println(<span class="keyword">super</span>.name + <span class="string">&quot;从早玩到晚&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>执行代码测试发现，调用了 Father 的 name</p>
</li>
<li><p>此时去修改 Son </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// 给 Son 添加一个 name 属性</span></span><br><span class="line"><span class="keyword">public</span> String name = <span class="string">&quot;悟天&quot;</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>再次执行代码发现，最终调用了 Son 的 name 属性</p>
</li>
</ul>
</li>
</ul>
<h5 id="在构造中使用-super-关键字"><a href="#在构造中使用-super-关键字" class="headerlink" title="在构造中使用 super 关键字"></a>在构造中使用 super 关键字</h5><ul>
<li><p>在构造函数中使用 super 关键字</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">GrandSon</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 在第一行默认会调用一个父类的 super() 构造</span></span><br><span class="line"><span class="comment">//        super();</span></span><br><span class="line">    <span class="comment">// 如果第一行中使用 this() 调用其他的自身构造时</span></span><br><span class="line">    <span class="comment">// super() 就不能在这个构造中使用</span></span><br><span class="line">    <span class="comment">// 因为 this() 和 super()都需要放在第一行</span></span><br><span class="line">    <span class="comment">// 所以在同一个构造函数中 this() 和 super() 只能出现一个</span></span><br><span class="line">    <span class="comment">// 如果希望调用父类的有参构造，那么必须显式调用</span></span><br><span class="line">    <span class="keyword">super</span>(<span class="string">&quot;二狗&quot;</span>,<span class="string">&quot;四驱兄弟&quot;</span>);</span><br><span class="line">    System.out.println(<span class="string">&quot;GrandSon 的无参构造被执行&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h5 id="关键字-this-和-super-总结"><a href="#关键字-this-和-super-总结" class="headerlink" title="关键字 this 和 super 总结"></a>关键字 this 和 super 总结</h5><ul>
<li>子类继承了父类所有的非私有属性和方法，可以直接调用</li>
<li>子类在构造时，一定会调用父类的构造，默认调用父类的无参构造。如果父类没有无参构造会报错，需要显式调用父类有参</li>
<li>如果子类希望指定调用父类的某个无参时，则需要显式调用：super(参数列表)</li>
<li>使用 super 和 tins 当作构造器使用时，必须放在构造器的第一行，所以只能二选一</li>
<li>java 中所有类都是 Object 的子类，Object 是所有类的基类，Object 也称之为超类</li>
<li>子类对多只能继承一个父类(指直接继承)，java 是单继承的</li>
</ul>
<h4 id="方法的重写"><a href="#方法的重写" class="headerlink" title="方法的重写"></a>方法的重写</h4><ul>
<li><p>子类可以继承父类的方法，但是我们不总是希望我们的方法一摸一样的。总是会有些许改变，龙生九子各有不同，每个类都有每个类的特征</p>
</li>
<li><p>重写的定义</p>
<ul>
<li>子父类之间，方法名一致，参数列表一致，访问修饰符一致或不严于父类，返回值一致或是其子类(抛出的异常一致或是其子类)。只要满足以上条件都可以称之为重写(override)</li>
<li>注意：返回值和访问修饰符(异常)不满足，其他都满足，编译报错</li>
</ul>
</li>
<li><p>重写的意义</p>
<ul>
<li>原有的方法无法满足新的需求，需要对这个方法进行改良来满足新的需求。重写是出现在子类继承父类的方法中。重写就是子类沿袭了父类的功能名称，又根据子类的需求重新的实现了该方法，从而进行拓展增强</li>
</ul>
</li>
<li><p>代码展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 子孙类的 eat 方法，重写了 Father 中的 eat 方法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">eat</span><span class="params">()</span></span>&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;不挑食，不爆食，身体好&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h5 id="方法的重写和重载区别"><a href="#方法的重写和重载区别" class="headerlink" title="方法的重写和重载区别"></a>方法的重写和重载区别</h5><table>
<thead>
<tr>
<th align="center">名称</th>
<th align="center">范围</th>
<th align="center">方法名</th>
<th align="center">参数列表</th>
<th align="center">返回类型</th>
<th align="center">权限修饰</th>
</tr>
</thead>
<tbody><tr>
<td align="center">重载(overload)</td>
<td align="center">本类</td>
<td align="center">一致</td>
<td align="center">类型，个数或顺序不同</td>
<td align="center">无关</td>
<td align="center">无关</td>
</tr>
<tr>
<td align="center">重写(override)</td>
<td align="center">子父类</td>
<td align="center">一致</td>
<td align="center">一致</td>
<td align="center">一致或是其子类</td>
<td align="center">子类不能严于父类</td>
</tr>
</tbody></table>
<h4 id="final-关键字"><a href="#final-关键字" class="headerlink" title="final 关键字"></a>final 关键字</h4><ul>
<li>final 关键字：是一种修饰符，代表不可以被改变，最终的</li>
<li>final 可以修饰：类、方法、成员变量、局部变量<ul>
<li>final 修饰类：类不能被继承(太监类)</li>
<li>final 修饰方法：表示方法是最终的，不能被覆写(重写)，但可以被重载</li>
<li>final 修饰成员变量：赋值后值不能改变，必须手动赋值，或者所有的构造方法中都能给其赋值</li>
<li>final 修饰局部变量：可以初始不赋值，后续使用之前必须进行一次赋值</li>
</ul>
</li>
<li>注意！final 关键字不能和 abstract 一起使用</li>
</ul>
<h4 id="超类-Object"><a href="#超类-Object" class="headerlink" title="超类 Object"></a>超类 Object</h4><ul>
<li><p>所有类的只要不使用 extends 关键字都会默认继承 Object 类</p>
</li>
<li><p>在 Object 中方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">package</span> java.lang;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Object</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 这两个先不管</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">native</span> <span class="keyword">void</span> <span class="title">registerNatives</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="keyword">static</span> &#123;</span><br><span class="line">        registerNatives();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 学习反射时候会用到，暂时不管，被 native 修饰说明是本地方法被 C++ 实现</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">native</span> Class&lt;?&gt; getClass();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 获取一个哈希值，也是本地方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">native</span> <span class="keyword">int</span> <span class="title">hashCode</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 对象比较的方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">equals</span><span class="params">(Object obj)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> (<span class="keyword">this</span> == obj);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 克隆对象，本地方法</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">native</span> Object <span class="title">clone</span><span class="params">()</span> <span class="keyword">throws</span> CloneNotSupportedException</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 打印对象，print 方法底层调用对象 toString</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> getClass().getName() + <span class="string">&quot;@&quot;</span> + Integer.toHexString(hashCode());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 与线程相关，暂时不用看</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">native</span> <span class="keyword">void</span> <span class="title">notify</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 与线程相关，暂时不用看</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">native</span> <span class="keyword">void</span> <span class="title">notifyAll</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 与线程相关，暂时不用看</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">native</span> <span class="keyword">void</span> <span class="title">wait</span><span class="params">(<span class="keyword">long</span> timeout)</span> <span class="keyword">throws</span> InterruptedException</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 与线程相关，暂时不用看</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">void</span> <span class="title">wait</span><span class="params">(<span class="keyword">long</span> timeout, <span class="keyword">int</span> nanos)</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">        。。。</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 与线程相关，暂时不用看</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">void</span> <span class="title">wait</span><span class="params">()</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">        wait(<span class="number">0</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// JDK 1.9 中被弃用，需要自己重写，但是不推荐使用</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">finalize</span><span class="params">()</span> <span class="keyword">throws</span> Throwable </span>&#123; &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>目前主要了解：hashCode、equals、toString、clone</p>
</li>
</ul>
<h5 id="hashCode"><a href="#hashCode" class="headerlink" title="hashCode"></a>hashCode</h5><ul>
<li><p>所有带 native 的方法都是本地方法，它不是 java 代码实现的</p>
</li>
<li><p>这个 hashcode 的返回值是这个实例的运行时内存地址</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 打印对象的 hashCode</span></span><br><span class="line"><span class="comment">// hashCode 值是一个经过 hash 算法后获取的值，大约(0-41亿左右)</span></span><br><span class="line"><span class="comment">// 该数值指向对象的内存地址，内存地址的计算实现了 hashCode 的算法</span></span><br><span class="line">Teacher teacher = <span class="keyword">new</span> Teacher</span><br><span class="line">                (<span class="string">&quot;帅气&quot;</span>,<span class="string">&quot;男&quot;</span>,<span class="number">25</span>,<span class="number">3000.0</span>);</span><br><span class="line">System.out.println(teacher.hashCode());</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>什么是 hash 算法：</p>
<ul>
<li>一般来讲 hash 翻译为 散列 ，也可以直接音译为 哈希 。目的就是把任意长度的输入(又叫做预映射)，通过散列算法，变成固定长度的输出，该输出的值就是散列值</li>
<li>最简单的散列算法：取余，固定长度输出<ul>
<li>123523 % 10 = 3</li>
<li>75234 % 10 = 4</li>
<li>876 % 10 = 6</li>
<li>53414 %10 = 4</li>
</ul>
</li>
</ul>
</li>
<li><p>hash 算法的特点</p>
<ul>
<li>只能通过原文计算出 hash 值，而且每次计算都一样，不能通过 hash 值计算原文</li>
<li>原文的微小变化就能将 hash 值发生巨大变化</li>
<li>一个好的 hash 算法还可以尽可能的避免发生 hash 值重复问题，也叫 hash 碰撞</li>
</ul>
</li>
<li><p>hash 的用途</p>
<ul>
<li>密码保护</li>
<li>文件校验</li>
</ul>
</li>
<li><p>常见的 hash 算法</p>
<ul>
<li>MD5 加密算法</li>
<li>SHA-0 加密算法</li>
<li>SHA-1 加密算法</li>
<li>SHA-256 加密算法</li>
<li>SHA-512 加密算法</li>
</ul>
</li>
<li><p>String 的 hash 算法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// String 的哈希算法，计算 &quot;ab&quot;</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">hashCode</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 定义一个变量，默认为 0</span></span><br><span class="line">    <span class="keyword">int</span> h = hash;</span><br><span class="line">    <span class="comment">// 判断字符串有没有内容</span></span><br><span class="line">    <span class="keyword">if</span> (h == <span class="number">0</span> &amp;&amp; value.length &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="comment">// 使用 char 数组接收字符串内容 [a,b]</span></span><br><span class="line">        <span class="keyword">char</span> val[] = value;</span><br><span class="line">		<span class="comment">// 循环计算 hashcode</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; value.length; i++) &#123;</span><br><span class="line">            <span class="comment">// 1：31*0 + 97 = 97</span></span><br><span class="line">            <span class="comment">// 2：31*97 + 98 = 3105</span></span><br><span class="line">            h = <span class="number">31</span> * h + val[i];</span><br><span class="line">        &#125;</span><br><span class="line">        hash = h;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> h;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>为什么 String 使用的是 31 作为乘数</p>
<ul>
<li>31 是奇素数，如果数字太小容易出现重复，大了容易溢出</li>
<li>31 和33的计算速度和哈希分布基本一致</li>
<li>String 的 hash 算法就是参考了 times 33</li>
</ul>
</li>
</ul>
<h5 id="equals"><a href="#equals" class="headerlink" title="equals"></a>equals</h5><ul>
<li><p>用于比较两个对象，在字符串中就重写了 equals 方法，用于比较两个字符串是否相同</p>
<ul>
<li><p>Object 中的 equals 使用 == 进行比较</p>
</li>
<li><p>String 中的 equals </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">equals</span><span class="params">(Object anObject)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 判断引用类型内容地址是否相同</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">this</span> == anObject) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 判断是否为相同类型</span></span><br><span class="line">    <span class="keyword">if</span> (anObject <span class="keyword">instanceof</span> String) &#123;</span><br><span class="line">        <span class="comment">// 将传入内容进行强转</span></span><br><span class="line">        String anotherString = (String)anObject;</span><br><span class="line">        <span class="keyword">int</span> n = value.length;</span><br><span class="line">        <span class="comment">// 先判断两个字符长度是否相同</span></span><br><span class="line">        <span class="keyword">if</span> (n == anotherString.value.length) &#123;</span><br><span class="line">            <span class="keyword">char</span> v1[] = value;</span><br><span class="line">            <span class="keyword">char</span> v2[] = anotherString.value;</span><br><span class="line">            <span class="keyword">int</span> i = <span class="number">0</span>;</span><br><span class="line">            <span class="comment">// 通过循环取出其中字符一一比较</span></span><br><span class="line">            <span class="keyword">while</span> (n-- != <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> (v1[i] != v2[i])</span><br><span class="line">                    <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">                i++;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>如果两个引用类型之间进行比较，使用 equals ，默认情况下都会区比较引用地址，这不是我们所希望的。因该是，引用类型其中指定的某些属性进行比较，如果相同则判断相同。此时就需要自己重写 equals 方法</p>
</li>
<li><p>例如比较比较学生类</p>
<ul>
<li><p>创建 Student 类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Student</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 学生 id</span></span><br><span class="line">    <span class="keyword">private</span> Integer stuid;</span><br><span class="line">    <span class="comment">// 学生姓名</span></span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="comment">// 学生年龄</span></span><br><span class="line">    <span class="keyword">private</span> Integer age;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Student</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Student</span><span class="params">(Integer stuid, String name, Integer age)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.stuid = stuid;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">        <span class="keyword">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">equals</span><span class="params">(Object obj)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 判断是否为相同引用</span></span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span> == obj)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 判断数据类型是否相同</span></span><br><span class="line">        <span class="keyword">if</span>(obj <span class="keyword">instanceof</span> Student)&#123;</span><br><span class="line">            <span class="comment">// 如果进入 if 说明一定不是 null 且类型相同</span></span><br><span class="line">            <span class="comment">// 进行强制类型转换</span></span><br><span class="line">            Student stu = (Student)obj;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.getStuid().equals(stu.getStuid()) &amp;&amp;</span><br><span class="line">                    <span class="keyword">this</span>.getName().equals(stu.getName()))&#123;</span><br><span class="line">                <span class="comment">// 如果 id 和 name 都相同，则判断为同一个学生</span></span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Integer <span class="title">getStuid</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> stuid;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setStuid</span><span class="params">(Integer stuid)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.stuid = stuid;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Integer <span class="title">getAge</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAge</span><span class="params">(Integer age)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>总结：</p>
<ul>
<li>基本类型比较使用 == ，比较的是字面值</li>
<li>引用类型比较 == ，比较的是地址值， equals 在不重写的情况下是继承自Object类的 equals 方法，他的实现和 == 相同，比较地址值</li>
<li>根据业务需求重写 equals 方法，按照需求方式来进行比较。例如：String 就重写了  equals 方法，比较的是存储的每个字符的值</li>
<li>在 java 中推荐重写 equals 方法，也需要重写一个 hash Code 方法，目的是为了保证一个对象如果 equals 相同，那么他们的逻辑内存地址(hashCode) 也应该相同</li>
</ul>
</li>
</ul>
<h5 id="toString-NaN"><a href="#toString-NaN" class="headerlink" title="toString"></a>toString</h5><ul>
<li><p>这个方法就是把一个实例对象转化成一个可以打印的字符串</p>
</li>
<li><p>重写 Student 中的 toString 使其打印出学生相关信息</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Student</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 学生 id</span></span><br><span class="line">    <span class="keyword">private</span> Integer stuid;</span><br><span class="line">    <span class="comment">// 学生姓名</span></span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="comment">// 学生年龄</span></span><br><span class="line">    <span class="keyword">private</span> Integer age;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Student</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Student</span><span class="params">(Integer stuid, String name, Integer age)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.stuid = stuid;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">        <span class="keyword">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Student&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;stuid=&quot;</span> + stuid +</span><br><span class="line">                <span class="string">&quot;, name=&#x27;&quot;</span> + name + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&quot;, age=&quot;</span> + age +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">//    public String toString()&#123;</span></span><br><span class="line"><span class="comment">//        // 类名&#123;属性名=值,属性名=值,....&#125;</span></span><br><span class="line"><span class="comment">//        return &quot;Student&#123;stuid = &quot;+ stuid +&quot;, &quot; +</span></span><br><span class="line"><span class="comment">//                &quot;name = &quot;+name+&quot;, &quot; +</span></span><br><span class="line"><span class="comment">//                &quot;age = &quot;+age+&quot;&#125;&quot;;</span></span><br><span class="line"><span class="comment">//    &#125;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">equals</span><span class="params">(Object o)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">this</span> == o) <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        <span class="keyword">if</span> (o == <span class="keyword">null</span> || getClass() != o.getClass()) <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        Student student = (Student) o;</span><br><span class="line">        <span class="keyword">return</span> Objects.equals(stuid, student.stuid) &amp;&amp;</span><br><span class="line">                Objects.equals(name, student.name);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">hashCode</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Objects.hash(stuid, name);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//    public boolean equals(Object obj)&#123;</span></span><br><span class="line"><span class="comment">//        // 判断是否为相同引用</span></span><br><span class="line"><span class="comment">//        if(this == obj)&#123;</span></span><br><span class="line"><span class="comment">//            return true;</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line"><span class="comment">//        // 判断数据类型是否相同</span></span><br><span class="line"><span class="comment">//        if(obj instanceof Student)&#123;</span></span><br><span class="line"><span class="comment">//            // 如果进入 if 说明一定不是 null 且类型相同</span></span><br><span class="line"><span class="comment">//            // 进行强制类型转换</span></span><br><span class="line"><span class="comment">//            Student stu = (Student)obj;</span></span><br><span class="line"><span class="comment">//            if(this.getStuid().equals(stu.getStuid()) &amp;&amp;</span></span><br><span class="line"><span class="comment">//                    this.getName().equals(stu.getName()))&#123;</span></span><br><span class="line"><span class="comment">//                // 如果 id 和 name 都相同，则判断为同一个学生</span></span><br><span class="line"><span class="comment">//                return true;</span></span><br><span class="line"><span class="comment">//            &#125;</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line"><span class="comment">//        return false;</span></span><br><span class="line"><span class="comment">//    &#125;</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Integer <span class="title">getStuid</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> stuid;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setStuid</span><span class="params">(Integer stuid)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.stuid = stuid;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Integer <span class="title">getAge</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAge</span><span class="params">(Integer age)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h5 id="clone"><a href="#clone" class="headerlink" title="clone"></a>clone</h5><ul>
<li>克隆方法就是在内存中赋值一个新的实例对象</li>
<li>实现克隆的步骤：<ul>
<li>对象实现 Cloneable 接口</li>
<li>重写 Object 中的 clone() 方法</li>
<li>在 clone 方法中调用 super.clone();</li>
</ul>
</li>
<li>浅克隆，克隆对象的引用属性的值如果改变，被克隆出来的对象相同引用属性的值也会随之改变</li>
<li>深克隆，克隆对象的引用属性的值如果改变，被克隆出来的对象相同引用属性的值不会随之改变。其实就是将引用属性再次进行自身克隆</li>
</ul>
<h3 id="面向对象之多态"><a href="#面向对象之多态" class="headerlink" title="面向对象之多态"></a>面向对象之多态</h3><ul>
<li><p>这是面向对象中最难的一个章节需要认值学习</p>
<ul>
<li><p>猫是动物吗？是</p>
</li>
<li><p>狗是动物吗？是</p>
</li>
<li><p>猫是狗吗？不是</p>
</li>
<li><p>代码中的表示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// 定义一个动物，它是猫</span></span><br><span class="line">Animal cat = <span class="keyword">new</span> Cat();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义一个动物，他是狗</span></span><br><span class="line">Aniaml dog = <span class="keyword">new</span> Dog();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 但是不能这样写</span></span><br><span class="line">Cat cat = <span class="keyword">new</span> Dog();</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>如果有个人需要养宠物</p>
<ul>
<li><p>猫可以作为宠物吗？</p>
</li>
<li><p>狗可以作为宠物吗？</p>
</li>
<li><p>代码展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Girl</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">pet</span><span class="params">(Animal animal)</span></span>&#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 调用</span></span><br><span class="line">girl.pet(<span class="keyword">new</span> Dog());</span><br><span class="line">girl.pet(<span class="keyword">new</span> Cat());</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果女孩只想养狗</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Girl</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">feed</span><span class="params">(Dog dog)</span></span>&#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 调用</span></span><br><span class="line">girl.pet(<span class="keyword">new</span> Dog());</span><br><span class="line">girl.pet(<span class="keyword">new</span> Cat()); 报错！</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>从代码层面理解多态</p>
<ul>
<li><p>修改 Girl 中的 feed 方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">feed</span><span class="params">(Animal animal)</span></span>&#123;</span><br><span class="line">    animal.actCute();</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>给 Animal 和他的子类添加 卖萌 的方法</p>
<ul>
<li><p>Animal</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actCute</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;动物在卖萌&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>Cat</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Cat</span> <span class="keyword">extends</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actCute</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;猫在卖萌&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>Dog</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Dog</span> <span class="keyword">extends</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actCute</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;狗在卖萌&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>测试代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    Girl girl = <span class="keyword">new</span> Girl();</span><br><span class="line">    <span class="comment">// 此时会输出什么？</span></span><br><span class="line">    girl.feed(<span class="keyword">new</span> Dog());</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>代码运行后测试结果发现是 狗在卖萌。问题在于，我们明明是定义了一个 Animal ，为什么会输出狗在卖萌呢？</p>
</li>
<li><p>在很多地方对于多态的描述有以下几个条件</p>
<ul>
<li>有继承</li>
<li>有方法的重写</li>
<li>有父类引用指向子类对象，父类类型 名称 = new 子类类型</li>
</ul>
</li>
<li><p>以上这种说法都是宏观层面对的</p>
</li>
</ul>
<h4 id="多态的底层原理"><a href="#多态的底层原理" class="headerlink" title="多态的底层原理"></a>多态的底层原理</h4><h5 id="字节码层分析"><a href="#字节码层分析" class="headerlink" title="字节码层分析"></a>字节码层分析</h5><ul>
<li><p>如果说现在有一段这样的个的代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">Animal animal = Math.random() &gt; <span class="number">0.5</span> ? <span class="keyword">new</span> Dog:<span class="keyword">new</span> Cat;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>这段代码，对于 animal 而言 能确定最终的类型是 Dog 还是 Cat 吗？明显不能，这就是 薛定谔的猫 ，百分之五十是 Dog，百分之五十是 Cat</p>
</li>
<li><p>对于 <code>Animal animal = new Cat();</code> 来说貌似等号左右都是确定的，但是对于虚拟机而言只有左侧是确定的不可变的，右侧代码不执行是不能确定的</p>
</li>
<li><p>那么我们可以叫 Animal 为 animal 的 <strong>静态类型</strong> ，或者叫编译类型、申明类型、外观类型。而右侧我们称之为 <strong>动态类型</strong> ，也叫 运行时类型、实际类型</p>
</li>
<li><p>在编译阶段 jvm 就能确定 静态类型。而动态类型必须等到运行时才能确定类型。对于运行时才能选择方法调用版本的行为称之为 <strong>虚方法分派</strong></p>
</li>
<li><p><strong>常量池就是我们的资源仓库，里面存储了大量的符号引用</strong>(类、方法、变量使用的符号)。其中一部分会在类加载阶段或者第一次使用时被转化为 <strong>直接引用</strong>， 这种转化称之为 <strong>静态解析</strong>。另一部分只会在运行期间转化为 <strong>直接引用</strong>，这一部分称之为 <strong>动态链接</strong></p>
<ul>
<li><p>静态解析</p>
<ul>
<li>简单来讲就是 类、方法、变量只是一个符号没有实际作用的。而代码运行后类进行加载阶段(此时代码没有执行)有一部分符号就已经被转化为 内存地址(直接引用) ，这种就称之为 静态解析</li>
</ul>
</li>
<li><p>动态链接</p>
<ul>
<li>某些一些类或方法会在代码真正执行期间进行转化 内存地址(直接引用)，这种称之为 动态链接</li>
</ul>
</li>
<li><p>代码展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 静态解析和动态链接</span></span><br><span class="line">Scanner sc = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line"><span class="comment">// 使用 Scanner 让用户输入信息</span></span><br><span class="line"><span class="keyword">int</span> num = sc.next();</span><br><span class="line"><span class="comment">// Animal 的类型是确认的</span></span><br><span class="line"><span class="comment">// 称之为静态解析</span></span><br><span class="line">Animal animal = <span class="keyword">null</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 到底是 Dog 还是 Cat 只有代码运行起来才能确认</span></span><br><span class="line"><span class="comment">// 称之为 动态链接</span></span><br><span class="line"><span class="keyword">if</span>(num==<span class="number">1</span>)&#123;</span><br><span class="line">    animal = <span class="keyword">new</span> Dog();</span><br><span class="line">&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    animal = <span class="keyword">new</span> Cat();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>代码示例</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Computer</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">plus</span><span class="params">(<span class="keyword">int</span> i,<span class="keyword">int</span> j)</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> i + j;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Computer computer = <span class="keyword">new</span> Computer();</span><br><span class="line">        System.out.println(computer.plus(<span class="number">3</span>, <span class="number">6</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>通过 java 通过的反编译工具 javap 查看字节码(javap -v 文件名.class)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 类文件所在路径</span></span><br><span class="line">Classfile /E:/sc20220501java/后端内容/project/javaSE/out/production/javaSE/com/seecen/day11/polymorphism/Computer.class</span><br><span class="line">  <span class="comment">// 最后修改时间 和 文件大小</span></span><br><span class="line">  Last modified <span class="number">2022</span>-<span class="number">7</span>-<span class="number">20</span>; size <span class="number">713</span> bytes</span><br><span class="line">  <span class="comment">// 该文件的 MD5 加密后的密码值</span></span><br><span class="line">  MD5 checksum 0dc2afd55a6e038fa651b39ecd9db7dd</span><br><span class="line">  <span class="comment">// 说明文件通过 Computer.java 编译而来</span></span><br><span class="line">  Compiled from <span class="string">&quot;Computer.java&quot;</span></span><br><span class="line"><span class="comment">// 类的相关信息</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">com</span>.<span class="title">seecen</span>.<span class="title">day11</span>.<span class="title">polymorphism</span>.<span class="title">Computer</span></span></span><br><span class="line"><span class="class">  <span class="title">minor</span> <span class="title">version</span>: 0</span></span><br><span class="line"><span class="class">  <span class="title">major</span> <span class="title">version</span>: 52</span></span><br><span class="line"><span class="class">  // 类的标记，访问修饰符为 <span class="title">public</span> ，支持父类方法调用</span></span><br><span class="line"><span class="class">  <span class="title">flags</span>: <span class="title">ACC_PUBLIC</span>, <span class="title">ACC_SUPER</span></span></span><br><span class="line"><span class="class">// 常量池</span></span><br><span class="line"><span class="class"><span class="title">Constant</span> <span class="title">pool</span>:</span></span><br><span class="line"><span class="class">   #1 </span>= Methodref          #<span class="number">7.</span>#<span class="number">27</span>         <span class="comment">// java/lang/Object.&quot;&lt;init&gt;&quot;:()V</span></span><br><span class="line">   #<span class="number">2</span> = Class              #<span class="number">28</span>            <span class="comment">// com/seecen/day11/polymorphism/Computer</span></span><br><span class="line">   #<span class="number">3</span> = Methodref          #<span class="number">2.</span>#<span class="number">27</span>         <span class="comment">// com/seecen/day11/polymorphism/Computer.&quot;&lt;init&gt;&quot;:()V</span></span><br><span class="line">   #<span class="number">4</span> = Fieldref           #<span class="number">29.</span>#<span class="number">30</span>        <span class="comment">// java/lang/System.out:Ljava/io/PrintStream;</span></span><br><span class="line">   #<span class="number">5</span> = Methodref          #<span class="number">2.</span>#<span class="number">31</span>         <span class="comment">// com/seecen/day11/polymorphism/Computer.plus:(II)I</span></span><br><span class="line">   #<span class="number">6</span> = Methodref          #<span class="number">32.</span>#<span class="number">33</span>        <span class="comment">// java/io/PrintStream.println:(I)V</span></span><br><span class="line">   #<span class="number">7</span> = Class              #<span class="number">34</span>            <span class="comment">// java/lang/Object</span></span><br><span class="line">   #<span class="number">8</span> = Utf8               &lt;init&gt;</span><br><span class="line">   #<span class="number">9</span> = Utf8               ()V</span><br><span class="line">  #<span class="number">10</span> = Utf8               Code</span><br><span class="line">  #<span class="number">11</span> = Utf8               LineNumberTable</span><br><span class="line">  #<span class="number">12</span> = Utf8               LocalVariableTable</span><br><span class="line">  #<span class="number">13</span> = Utf8               <span class="keyword">this</span></span><br><span class="line">  #<span class="number">14</span> = Utf8               Lcom/seecen/day11/polymorphism/Computer;</span><br><span class="line">  #<span class="number">15</span> = Utf8               plus</span><br><span class="line">  #<span class="number">16</span> = Utf8               (II)I</span><br><span class="line">  #<span class="number">17</span> = Utf8               i</span><br><span class="line">  #<span class="number">18</span> = Utf8               I</span><br><span class="line">  #<span class="number">19</span> = Utf8               j</span><br><span class="line">  #<span class="number">20</span> = Utf8               main</span><br><span class="line">  #<span class="number">21</span> = Utf8               ([Ljava/lang/String;)V</span><br><span class="line">  #<span class="number">22</span> = Utf8               args</span><br><span class="line">  #<span class="number">23</span> = Utf8               [Ljava/lang/String;</span><br><span class="line">  #<span class="number">24</span> = Utf8               computer</span><br><span class="line">  #<span class="number">25</span> = Utf8               SourceFile</span><br><span class="line">  #<span class="number">26</span> = Utf8               Computer.java</span><br><span class="line">  #<span class="number">27</span> = NameAndType        #<span class="number">8</span>:#<span class="number">9</span>          <span class="comment">// &quot;&lt;init&gt;&quot;:()V</span></span><br><span class="line">  #<span class="number">28</span> = Utf8               com/seecen/day11/polymorphism/Computer</span><br><span class="line">  #<span class="number">29</span> = Class              #<span class="number">35</span>            <span class="comment">// java/lang/System</span></span><br><span class="line">  #<span class="number">30</span> = NameAndType        #<span class="number">36</span>:#<span class="number">37</span>        <span class="comment">// out:Ljava/io/PrintStream;</span></span><br><span class="line">  #<span class="number">31</span> = NameAndType        #<span class="number">15</span>:#<span class="number">16</span>        <span class="comment">// plus:(II)I</span></span><br><span class="line">  #<span class="number">32</span> = Class              #<span class="number">38</span>            <span class="comment">// java/io/PrintStream</span></span><br><span class="line">  #<span class="number">33</span> = NameAndType        #<span class="number">39</span>:#<span class="number">40</span>        <span class="comment">// println:(I)V</span></span><br><span class="line">  #<span class="number">34</span> = Utf8               java/lang/Object</span><br><span class="line">  #<span class="number">35</span> = Utf8               java/lang/System</span><br><span class="line">  #<span class="number">36</span> = Utf8               out</span><br><span class="line">  #<span class="number">37</span> = Utf8               Ljava/io/PrintStream;</span><br><span class="line">  #<span class="number">38</span> = Utf8               java/io/PrintStream</span><br><span class="line">  #<span class="number">39</span> = Utf8               println</span><br><span class="line">  #<span class="number">40</span> = Utf8               (I)V</span><br><span class="line">&#123;</span><br><span class="line">  <span class="comment">// Computer 的无参构造</span></span><br><span class="line">  <span class="keyword">public</span> com.seecen.day11.polymorphism.Computer();</span><br><span class="line">    <span class="comment">// 方法描述，() 无参数 ，V 无返回值 void</span></span><br><span class="line">    descriptor: ()V</span><br><span class="line">    flags: ACC_PUBLIC</span><br><span class="line">	<span class="comment">// 方法中需要执行的代码</span></span><br><span class="line">    Code:</span><br><span class="line">      stack=<span class="number">1</span>, locals=<span class="number">1</span>, args_size=<span class="number">1</span></span><br><span class="line">         <span class="number">0</span>: aload_0</span><br><span class="line">		 <span class="comment">// 构造器编译之后也是符号引用，就是名字</span></span><br><span class="line">		 <span class="comment">// 构造器的调用没有什么特别的，确定是哪一个就是哪一个</span></span><br><span class="line">		 <span class="comment">// 这就是 静态解析</span></span><br><span class="line">		 <span class="comment">// invokespecial 调用父类的构造</span></span><br><span class="line">         <span class="number">1</span>: invokespecial #<span class="number">1</span>                  <span class="comment">// Method java/lang/Object.&quot;&lt;init&gt;&quot;:()V</span></span><br><span class="line">         <span class="number">4</span>: <span class="keyword">return</span></span><br><span class="line">      LineNumberTable:</span><br><span class="line">        line <span class="number">10</span>: <span class="number">0</span></span><br><span class="line">      LocalVariableTable:</span><br><span class="line">        Start  Length  Slot  Name   Signature</span><br><span class="line">            <span class="number">0</span>       <span class="number">5</span>     <span class="number">0</span>  <span class="keyword">this</span>   Lcom/seecen/day11/polymorphism/Computer;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// plus() 加法的方法表</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">plus</span><span class="params">(<span class="keyword">int</span>, <span class="keyword">int</span>)</span></span>;</span><br><span class="line">    descriptor: (II)I</span><br><span class="line">    flags: ACC_PUBLIC</span><br><span class="line">    Code:</span><br><span class="line">      stack=<span class="number">2</span>, locals=<span class="number">3</span>, args_size=<span class="number">3</span></span><br><span class="line">         <span class="number">0</span>: iload_1</span><br><span class="line">         <span class="number">1</span>: iload_2</span><br><span class="line">         <span class="number">2</span>: iadd</span><br><span class="line">         <span class="number">3</span>: ireturn</span><br><span class="line">      LineNumberTable:</span><br><span class="line">        line <span class="number">12</span>: <span class="number">0</span></span><br><span class="line">      LocalVariableTable:</span><br><span class="line">        Start  Length  Slot  Name   Signature</span><br><span class="line">            <span class="number">0</span>       <span class="number">4</span>     <span class="number">0</span>  <span class="keyword">this</span>   Lcom/seecen/day11/polymorphism/Computer;</span><br><span class="line">            <span class="number">0</span>       <span class="number">4</span>     <span class="number">1</span>     i   I</span><br><span class="line">            <span class="number">0</span>       <span class="number">4</span>     <span class="number">2</span>     j   I</span><br><span class="line"></span><br><span class="line">  <span class="comment">// main() 方法的方法表</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(java.lang.String[])</span></span>;</span><br><span class="line">    descriptor: ([Ljava/lang/String;)V</span><br><span class="line">    flags: ACC_PUBLIC, ACC_STATIC</span><br><span class="line">    Code:</span><br><span class="line">      stack=<span class="number">4</span>, locals=<span class="number">2</span>, args_size=<span class="number">1</span></span><br><span class="line">         <span class="number">0</span>: <span class="keyword">new</span>           #<span class="number">2</span>                  <span class="comment">// class com/seecen/day11/polymorphism/Computer</span></span><br><span class="line">         <span class="number">3</span>: dup</span><br><span class="line">		 <span class="comment">// 调用构造方法</span></span><br><span class="line">         <span class="number">4</span>: invokespecial #<span class="number">3</span>                  <span class="comment">// Method &quot;&lt;init&gt;&quot;:()V</span></span><br><span class="line">         <span class="number">7</span>: astore_1</span><br><span class="line">         <span class="number">8</span>: getstatic     #<span class="number">4</span>                  <span class="comment">// Field java/lang/System.out:Ljava/io/PrintStream;</span></span><br><span class="line">        <span class="number">11</span>: aload_1</span><br><span class="line">        <span class="number">12</span>: iconst_3</span><br><span class="line">        <span class="number">13</span>: bipush        <span class="number">6</span></span><br><span class="line">		<span class="comment">// 调用 plus 方法</span></span><br><span class="line">		<span class="comment">// 1，本类中有没有这个方法，父类有，或者爷爷有。。。</span></span><br><span class="line">		<span class="comment">// 2，调用这个方法的实例对象在编译期是否能确定？未必！</span></span><br><span class="line">		<span class="comment">// invokevirtual 虚方法调用</span></span><br><span class="line">		<span class="comment">// 简单来讲就是最终 plus 方法是执行自己的还是父类的，不知道</span></span><br><span class="line">		<span class="comment">// 这也就意味着在加载阶段是不能确认的，只有根据运行时情况进行解析</span></span><br><span class="line">		<span class="comment">// 这就是 动态解析</span></span><br><span class="line">        <span class="number">15</span>: invokevirtual #<span class="number">5</span>                  <span class="comment">// Method plus:(II)I</span></span><br><span class="line">        <span class="number">18</span>: invokevirtual #<span class="number">6</span>                  <span class="comment">// Method java/io/PrintStream.println:(I)V</span></span><br><span class="line">        <span class="number">21</span>: <span class="keyword">return</span></span><br><span class="line">      LineNumberTable:</span><br><span class="line">        line <span class="number">16</span>: <span class="number">0</span></span><br><span class="line">        line <span class="number">17</span>: <span class="number">8</span></span><br><span class="line">        line <span class="number">18</span>: <span class="number">21</span></span><br><span class="line">      LocalVariableTable:</span><br><span class="line">        Start  Length  Slot  Name   Signature</span><br><span class="line">            <span class="number">0</span>      <span class="number">22</span>     <span class="number">0</span>  args   [Ljava/lang/String;</span><br><span class="line">            <span class="number">8</span>      <span class="number">14</span>     <span class="number">1</span> computer   Lcom/seecen/day11/polymorphism/Computer;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 主类文件 </span></span><br><span class="line">SourceFile: <span class="string">&quot;Computer.java&quot;</span></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>总结：在编译期间，类加载时能够直接确定的就称之为 静态解析，只有在代码运行后才能确认的称之为 动态链接。这里只会涉及到方法，与变量无关</p>
</li>
</ul>
<h5 id="方法在栈中的调用"><a href="#方法在栈中的调用" class="headerlink" title="方法在栈中的调用"></a>方法在栈中的调用</h5><ul>
<li><p>栈结构是一种先进后出的方式</p>
</li>
<li><p>现在修改以下我们的 Computer 代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">a</span><span class="params">()</span></span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">b</span><span class="params">()</span></span>&#123;</span><br><span class="line">    a();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">c</span><span class="params">()</span></span>&#123;</span><br><span class="line">    b();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    Computer computer = <span class="keyword">new</span> Computer();</span><br><span class="line">    computer.c();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 方法调用的顺序：</span></span><br><span class="line"><span class="comment">// c方法调用 ==》 b方法调用 ==》 a方法调用</span></span><br><span class="line"><span class="comment">// a方法结束 ==》 b方法结束 ==》 c方法结束</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>我们单独看看 plus 方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">  </span><br><span class="line">  </span><br><span class="line"> <span class="comment">// plus() 加法的方法表</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">plus</span><span class="params">(<span class="keyword">int</span>, <span class="keyword">int</span>)</span></span>;</span><br><span class="line">   <span class="comment">// 表示该方法有两个 int ，返回值为 int</span></span><br><span class="line">   descriptor: (II)I</span><br><span class="line">   flags: ACC_PUBLIC</span><br><span class="line"><span class="comment">// 具体的代码指令</span></span><br><span class="line">   Code:</span><br><span class="line">  <span class="comment">// 栈的深度为2，本地变量为3，参数有3个</span></span><br><span class="line">     stack=<span class="number">2</span>, locals=<span class="number">3</span>, args_size=<span class="number">3</span></span><br><span class="line">	 <span class="comment">// 将第二个int型本地变量推送至栈顶</span></span><br><span class="line">        <span class="number">0</span>: iload_1</span><br><span class="line">	 <span class="comment">// 将第三个int型本地变量推送至栈顶</span></span><br><span class="line">        <span class="number">1</span>: iload_2</span><br><span class="line">	 <span class="comment">// 将栈顶两int型数值相加并将结果压入栈顶</span></span><br><span class="line">        <span class="number">2</span>: iadd</span><br><span class="line">	 <span class="comment">// 从当前方法返回int</span></span><br><span class="line">        <span class="number">3</span>: ireturn</span><br><span class="line">     LineNumberTable:</span><br><span class="line">       line <span class="number">12</span>: <span class="number">0</span></span><br><span class="line">  <span class="comment">// 本地变量表</span></span><br><span class="line">     LocalVariableTable:</span><br><span class="line">       Start  Length  Slot  Name   Signature</span><br><span class="line">           <span class="number">0</span>       <span class="number">4</span>     <span class="number">0</span>  <span class="keyword">this</span>   Lcom/seecen/day11/polymorphism/Computer;</span><br><span class="line">           <span class="number">0</span>       <span class="number">4</span>     <span class="number">1</span>     i   I</span><br><span class="line">           <span class="number">0</span>       <span class="number">4</span>     <span class="number">2</span>     j   I</span><br><span class="line">  </span><br><span class="line">  </span><br></pre></td></tr></table></figure></li>
<li><p>再来看看 main 方法的</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line">  </span><br><span class="line">  </span><br><span class="line"> <span class="comment">// main() 方法的方法表</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(java.lang.String[])</span></span>;</span><br><span class="line">   <span class="comment">// 表示为方法参数有一个 String类型的数值([表示数组，L表示引用类型) 没有返回值</span></span><br><span class="line">   descriptor: ([Ljava/lang/String;)V</span><br><span class="line"><span class="comment">// 表示方法是 public 和 static</span></span><br><span class="line">   flags: ACC_PUBLIC, ACC_STATIC</span><br><span class="line"><span class="comment">// 具体代码指令</span></span><br><span class="line">   Code:</span><br><span class="line">  <span class="comment">// 操作栈深度为4，本地变量为2，参数列为1</span></span><br><span class="line">     stack=<span class="number">4</span>, locals=<span class="number">2</span>, args_size=<span class="number">1</span></span><br><span class="line">	 <span class="comment">// 创建一个对象，并将其引用值压入栈顶</span></span><br><span class="line">        <span class="number">0</span>: <span class="keyword">new</span>           #<span class="number">2</span>                  <span class="comment">// class com/seecen/day11/polymorphism/Computer</span></span><br><span class="line">        <span class="comment">// 复制栈顶数值并将复制值压入栈顶</span></span><br><span class="line">	 <span class="number">3</span>: dup</span><br><span class="line">	 <span class="comment">// 调用超类构造方法，实例初始化方法，私有方法</span></span><br><span class="line">        <span class="number">4</span>: invokespecial #<span class="number">3</span>                  <span class="comment">// Method &quot;&lt;init&gt;&quot;:()V</span></span><br><span class="line">	 <span class="comment">// 将栈顶引用型数值存入第二个本地变量</span></span><br><span class="line">        <span class="number">7</span>: astore_1</span><br><span class="line">	 <span class="comment">// 获取指定类的静态域，并将其值压入栈顶</span></span><br><span class="line">        <span class="number">8</span>: getstatic     #<span class="number">4</span>                  <span class="comment">// Field java/lang/System.out:Ljava/io/PrintStream;</span></span><br><span class="line">	<span class="comment">// 将第二个引用类型本地变量推送至栈顶</span></span><br><span class="line">       <span class="number">11</span>: aload_1</span><br><span class="line">	<span class="comment">// 将int型3推送至栈顶，就是参数 3 </span></span><br><span class="line">       <span class="number">12</span>: iconst_3</span><br><span class="line">	<span class="comment">// 将单字节的常量值(-128~127)推送至栈顶</span></span><br><span class="line">       <span class="number">13</span>: bipush        <span class="number">6</span></span><br><span class="line">	<span class="comment">// 调用实例方法 ，调用 plus 方法</span></span><br><span class="line">	<span class="comment">// 1，本类中有没有这个方法，父类有，或者爷爷有。。。</span></span><br><span class="line">	<span class="comment">// 2，调用这个方法的实例对象在编译期是否能确定？未必！</span></span><br><span class="line">	<span class="comment">// invokevirtual 虚方法调用</span></span><br><span class="line">	<span class="comment">// 简单来讲就是最终 plus 方法是执行自己的还是父类的，不知道</span></span><br><span class="line">	<span class="comment">// 这也就意味着在加载阶段是不能确认的，只有根据运行时情况进行解析</span></span><br><span class="line">	<span class="comment">// 这就是 动态解析</span></span><br><span class="line">       <span class="number">15</span>: invokevirtual #<span class="number">5</span>                  <span class="comment">// Method plus:(II)I</span></span><br><span class="line">			  <span class="comment">// plus() 加法的方法表</span></span><br><span class="line">			  <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">plus</span><span class="params">(<span class="keyword">int</span>, <span class="keyword">int</span>)</span></span>;</span><br><span class="line">				<span class="comment">// 表示该方法有两个 int ，返回值为 int</span></span><br><span class="line">				descriptor: (II)I</span><br><span class="line">				flags: ACC_PUBLIC</span><br><span class="line">				<span class="comment">// 具体的代码指令</span></span><br><span class="line">				Code:</span><br><span class="line">				  <span class="comment">// 栈的深度为2，本地变量为3，参数有3个</span></span><br><span class="line">				  stack=<span class="number">2</span>, locals=<span class="number">3</span>, args_size=<span class="number">3</span></span><br><span class="line">					 <span class="comment">// 将第二个int型本地变量推送至栈顶</span></span><br><span class="line">					 <span class="number">0</span>: iload_1</span><br><span class="line">					 <span class="comment">// 将第三个int型本地变量推送至栈顶</span></span><br><span class="line">					 <span class="number">1</span>: iload_2</span><br><span class="line">					 <span class="comment">// 将栈顶两int型数值相加并将结果压入栈顶</span></span><br><span class="line">					 <span class="number">2</span>: iadd</span><br><span class="line">					 <span class="comment">// 从当前方法返回int</span></span><br><span class="line">					 <span class="number">3</span>: ireturn</span><br><span class="line">				  LineNumberTable:</span><br><span class="line">					line <span class="number">12</span>: <span class="number">0</span></span><br><span class="line">				  <span class="comment">// 本地变量表</span></span><br><span class="line">				  LocalVariableTable:</span><br><span class="line">					Start  Length  Slot  Name   Signature</span><br><span class="line">						<span class="number">0</span>       <span class="number">4</span>     <span class="number">0</span>  <span class="keyword">this</span>   Lcom/seecen/day11/polymorphism/Computer;</span><br><span class="line">						<span class="number">0</span>       <span class="number">4</span>     <span class="number">1</span>     i   I</span><br><span class="line">						<span class="number">0</span>       <span class="number">4</span>     <span class="number">2</span>     j   I</span><br><span class="line">       <span class="number">18</span>: invokevirtual #<span class="number">6</span>                  <span class="comment">// Method java/io/PrintStream.println:(I)V</span></span><br><span class="line">       <span class="comment">// 从当前方法返回void</span></span><br><span class="line">	<span class="number">21</span>: <span class="keyword">return</span></span><br><span class="line">     LineNumberTable:</span><br><span class="line">       line <span class="number">16</span>: <span class="number">0</span></span><br><span class="line">       line <span class="number">17</span>: <span class="number">8</span></span><br><span class="line">       line <span class="number">18</span>: <span class="number">21</span></span><br><span class="line">     LocalVariableTable:</span><br><span class="line">       Start  Length  Slot  Name   Signature</span><br><span class="line">           <span class="number">0</span>      <span class="number">22</span>     <span class="number">0</span>  args   [Ljava/lang/String;</span><br><span class="line">           <span class="number">8</span>      <span class="number">14</span>     <span class="number">1</span> computer   Lcom/seecen/day11/polymorphism/Computer;</span><br><span class="line">                                    </span><br><span class="line">  </span><br></pre></td></tr></table></figure></li>
<li><p>在字节码中我们可以看到 invokespecial 和 invokevirtual 这样的指令用来调用方法的，当然还有 invokestatic 和 invokeinterface 和 invokedynamic </p>
</li>
<li><p>invokespecial ：调用构造方法，私有方法</p>
</li>
<li><p>invokestatic ：调用静态方法</p>
</li>
<li><p>invokevirtual ：调用实例方法(虚方法)</p>
</li>
<li><p>invokeinterface ： 调用接口方法(虚方法)</p>
</li>
<li><p>invokedynamic ：调用动态链接方法(虚方法)</p>
<ul>
<li>java8支持动态类型语言，开始支持 invokedynamic </li>
<li>lambda 表达式就会生成 invokedynamic 指令</li>
</ul>
</li>
<li><p>当使用 invokespecial 和 invokestatic 时，调用 构造方法，私有方法，静态方法 和 被 final 修饰的方法。都会在类加载期间能够顺序解析称为直接引用，这一类方法叫做 <strong>非虚方法</strong> 。反之其他的都是 <strong>虚方法</strong> 。注意：被 final 修饰的方法依然需要使用 invokevirtual 调用，历史遗留问题</p>
</li>
<li><p>总结：如果以上概念不能理解可以直接记住一下口诀</p>
<ul>
<li>成员变量：编译看左边，运行还看左边</li>
<li>成员方法：编译看左边，运行看右边</li>
<li>编译：把 .java 编译成 .class 文件</li>
</ul>
</li>
</ul>
<h4 id="重载方法的调用"><a href="#重载方法的调用" class="headerlink" title="重载方法的调用"></a>重载方法的调用</h4><ul>
<li><p>我们在调用一个虚方法时， jvm 会在适当的时候帮我们选择一个合适版本的方法。有时候是在编译期，有时候在运行时，这个方法的版本选择我们可以称之为 <strong>方法分派</strong> </p>
</li>
<li><p>示例代码</p>
<ul>
<li><p>创建 Human</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Human</span> </span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>创建 Man</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Man</span> <span class="keyword">extends</span> <span class="title">Human</span> </span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>创建 Woman</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Woman</span> <span class="keyword">extends</span> <span class="title">Human</span> </span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>创建 Party</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Party</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">play</span><span class="params">(Human human)</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;人类的狂欢&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">play</span><span class="params">(Man man)</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;男人的狂欢&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">play</span><span class="params">(Woman woman)</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;女人的狂欢&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Party party = <span class="keyword">new</span> Party();</span><br><span class="line">        Human human = <span class="keyword">new</span> Human();</span><br><span class="line">        party.play(human);</span><br><span class="line">        Human man = <span class="keyword">new</span> Man();</span><br><span class="line">        party.play(man);</span><br><span class="line">        Human woman = <span class="keyword">new</span> Woman();</span><br><span class="line">        party.play(woman);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>最终结果为</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">人类的狂欢</span><br><span class="line">人类的狂欢</span><br><span class="line">人类的狂欢</span><br><span class="line">    </span><br></pre></td></tr></table></figure></li>
<li><p>发现最终结果是三个 人类的狂欢，这与我们的预期有所出入</p>
</li>
<li><p>实际上虚拟机在选择重载方法时，是通过 静态类型 决定的而不是 动态类型 。由于 静态类型 在编译期间就一级可以确定，事实上虚拟机在编译期间就已经知道选择哪一个重载方法了，并且将这个方法的符号引用写在了 invokevirtual 的指令中</p>
</li>
<li><p>所有依赖 静态类型 决定方法执行版本的分派动作称之为 静态分派，有些人将这个过程称之为 解析而不是分派。JVM会帮助我们选择一个合适方法法，选择它认为最匹配的版本。也是因为确实存在诸如自动拆装箱，对象向上转型等问题</p>
</li>
</ul>
</li>
<li><p>如果理解了，再看下面这个实例</p>
<ul>
<li><p>创建 Overload 类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Overload</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sayHello</span><span class="params">(Object arg)</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;hello Object&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sayHello</span><span class="params">(<span class="keyword">int</span> arg)</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;hello int&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sayHello</span><span class="params">(<span class="keyword">long</span> arg)</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;hello long&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sayHello</span><span class="params">(Character arg)</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;hello Character&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sayHello</span><span class="params">(<span class="keyword">char</span> arg)</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;hello char&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sayHello</span><span class="params">(<span class="keyword">char</span>...arg)</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;hello char...&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">new</span> Overload().sayHello(<span class="string">&#x27;a&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>最终执行结果</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">执行后结果为：</span><br><span class="line">    hello <span class="keyword">char</span></span><br><span class="line"></span><br><span class="line">如果我们注释掉 sayHello(<span class="keyword">char</span> arg) 方法呢？</span><br><span class="line">    </span><br><span class="line">一一注释后顺序为：</span><br><span class="line">    hello <span class="keyword">int</span></span><br><span class="line">    hello <span class="keyword">long</span></span><br><span class="line">    hello Character</span><br><span class="line">    hello Object</span><br><span class="line">    hello <span class="keyword">char</span>...</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>很多说重载不属于多态，因为代码的编译期，方法就已经通过 静态分派 确认了。但是有人说因为重载是通过一个相同的方法名进行调用，得到不同的结果，也把这种行为叫做多态(静态多态) 。以上说法都对，都无所谓</p>
</li>
<li><p>总结：</p>
<ul>
<li>重载时看参数的静态特征，通过 静态分派 匹配最优方法</li>
</ul>
</li>
</ul>
<h4 id="重写方法的调用"><a href="#重写方法的调用" class="headerlink" title="重写方法的调用"></a>重写方法的调用</h4><ul>
<li>这个重写方法的调用过程以及在上面说的很明白了</li>
<li>invokevirtual 指令在执行的时候顺序如下：<ul>
<li>找到栈顶的元素所指向的实际类型(实例的内存地址)，例如：Dog 实例</li>
<li>根据内存地址，在 Dog 实例中找到调用的方法，如果找到了，直接调用</li>
<li>如果找不到，将由下至上沿着继承关系，向父类查找</li>
</ul>
</li>
</ul>
<h4 id="重写和重载"><a href="#重写和重载" class="headerlink" title="重写和重载"></a>重写和重载</h4><ul>
<li><p>重载只是选择了调用方法的版本(静态特征)</p>
</li>
<li><p>重写是具体明确了调用谁的方法(动态特征)</p>
</li>
<li><p>如果上面理解比较吃力，可以记住下面的总结</p>
<ul>
<li>如果方法出现了又重载又重写，计算机会先确定方法的版本，再确定调用谁的方法</li>
<li>如果有重写，我们可以先看方法的重写调用，如果发生了重载再通过静态特征来确认最终的执行方法</li>
</ul>
</li>
<li><p>看以下示例代码</p>
<ul>
<li><p>修改 Animal 类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String name = <span class="string">&quot;不知名动物&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actCute</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(name + <span class="string">&quot;动物在卖萌&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">eat</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;animal 在吃东西&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">eat</span><span class="params">(String food)</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;animal 在吃&quot;</span>+food);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>修改 Dog 类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Dog</span> <span class="keyword">extends</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String name = <span class="string">&quot;一只修勾&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actCute</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(name + <span class="string">&quot;狗在卖萌&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">eat</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;dog 在吃东西&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">eat</span><span class="params">(String food)</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;dog 在吃&quot;</span>+food);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Animal animal = <span class="keyword">new</span> Dog();</span><br><span class="line">        animal.eat(<span class="string">&quot;香蕉&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>这里就是先确认了方法的版本，再去通过 invokevirtual 确认最终的执行</p>
<ul>
<li>第一步确认方法的版本 <code>Animal::eat(String food)V</code> ，静态分派</li>
<li>第二部进行动态分派，根据运行时类型确认具体调用谁的 eat:(String food) 。因为运行时类是 Dog，所以最终方法选择是 <code>Dog::eat(String food)V</code> </li>
</ul>
</li>
<li><p>这两个过程是相辅相成的</p>
</li>
<li><p>重载和重写是 java 多态的体现，但是由于重载方法是静态分派的原有，有些人就不认为重载是多态的体现</p>
</li>
</ul>
</li>
<li><p>最后一定要注意！<strong>多态只和方法有关和属性无关</strong> ！</p>
</li>
</ul>
<h4 id="对象转型"><a href="#对象转型" class="headerlink" title="对象转型"></a>对象转型</h4><ul>
<li>向上转型：子类型对象转为父类型，向上转型不需要显示转化<ul>
<li>Father father = son;</li>
<li>向上转型会丢失子类独有的特征</li>
</ul>
</li>
<li>向下转型：父类型对象转为子类型，向下转型需要强制转化<ul>
<li>Son son = (son) father;</li>
<li>向下转型可能会出现错误，需要谨慎！</li>
</ul>
</li>
<li>问题：为什么向上转型会丢失 Dog 的特性？<ul>
<li>因为方法的调用会先通过 静态分派 根据静态类型选择对应的版本，所以说此时只能看见 Animal 这个类中的方法，所以 Dog 的特有方法就丢失了</li>
<li>在代码运行时，还是会通过 动态分派 找到最终执行方法的实例，所以最终调用的方法依然是 Dog 类中的</li>
<li>由此得到一个结论，一个对象能够调用什么方法由 静态类型 决定，方法的执行由 动态类型 决定</li>
</ul>
</li>
</ul>
<h2 id="抽象类和接口"><a href="#抽象类和接口" class="headerlink" title="抽象类和接口"></a>抽象类和接口</h2><h3 id="抽象类"><a href="#抽象类" class="headerlink" title="抽象类"></a>抽象类</h3><ul>
<li><p>抽象类也是一个类，只不过这个类被 abstract 修饰，成为了一个抽象类</p>
</li>
<li><p>抽象类就是将一个类中的某些行为(方法)进行抽象表达，具体的实现交由子类完成</p>
<ul>
<li>例如：图形计算面积时(抽象方法)，每个图形计算方法不同<ul>
<li>图形计算面积这个方法，在不确定图形之前是不能够确定如何计算的，所以这个计算面积的方法需要抽象表达。</li>
</ul>
</li>
</ul>
</li>
<li><p>定义一个抽象类</p>
<ul>
<li>public abstract class 类名{}</li>
</ul>
</li>
<li><p>代码示例</p>
<ul>
<li><p>Animal 类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// 使用 abstract 关键字定义该类为抽象类</span></span><br><span class="line"><span class="comment">// 如果一个类中出现了 抽象方法 那么该类必须为抽象类！</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 抽象类中也可以有成员变量</span></span><br><span class="line">    <span class="keyword">public</span> String name;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Animal</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        name = <span class="string">&quot;ABC&quot;</span>;</span><br><span class="line">        System.out.println(<span class="string">&quot;Animal 抽象类执行了实例化方法&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 比如吃东西的方法，如果不确定到底是什么动物</span></span><br><span class="line"><span class="comment">     * 那么就不能确定最终是要吃什么东西</span></span><br><span class="line"><span class="comment">     * 也就是不需要该方法的实现代码块</span></span><br><span class="line"><span class="comment">     * 这个方法就是抽象方法，需要使用 abstract 关键字修饰</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">eat</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">    </span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 抽象类中除了抽象方法也可以有普通方法</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">print</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;我是一个动物&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 抽象类不能被实例化</span></span><br><span class="line">        <span class="comment">// 因为其中存在抽象方法，没有实现无法调用</span></span><br><span class="line"><span class="comment">//        Animal animal = new Animal();</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>Cat 类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// Cat 继承了 Animal 抽象类，必须完成约定否则自己本身也是抽象类</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Cat</span> <span class="keyword">extends</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Cat</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">super</span>();</span><br><span class="line">        System.out.println(<span class="string">&quot;Cat 的实例被创建了！&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 如果继承了抽象类，必须实现抽象类中的抽象方法</span></span><br><span class="line"><span class="comment">     *  或者自己也是一个抽象类</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">eat</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(name + <span class="string">&quot;在吃东西&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 使用多态创建实例</span></span><br><span class="line">        Animal tom = <span class="keyword">new</span> Cat();</span><br><span class="line">        tom.eat();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>小问题：抽象类可以使用 final 声明吗？</p>
<ul>
<li>抽象类存在的目的就是为了让子类继承的，一个类被 final 修饰后就是去了被继承的能力，结果当然不行。抽象方法也是相同的道理</li>
</ul>
</li>
</ul>
<h4 id="抽象类小练习"><a href="#抽象类小练习" class="headerlink" title="抽象类小练习"></a>抽象类小练习</h4><ul>
<li>电脑的抽象类(abstract)：品牌属性私有，开机抽象方法，关机抽象方法<ul>
<li>电脑子类<ul>
<li>华为电脑，实现电脑的抽象类，通过自身的无参构造给父类的平品牌赋值</li>
<li>小米电脑，实现电脑的抽象类，通过自身的无参构造给父类的平品牌赋值</li>
</ul>
</li>
</ul>
</li>
<li>调用实现类的开机关机方法展示：xxx 品牌的电脑开机成功/关机成功</li>
</ul>
<h4 id="抽象类特点"><a href="#抽象类特点" class="headerlink" title="抽象类特点"></a>抽象类特点</h4><ul>
<li>抽象方法必须写在抽象类中</li>
<li>抽象类不一定要写抽象方法</li>
<li>抽象方法必须被 public 或 protected 和不写(默认)，如果设置为 private 不能被子类继承，会报错<ul>
<li>JDK 1.8以前，抽象类的方法默认访问权限为 protected</li>
<li>JDK 1.8时，抽象类的方法默认访问权限变为 default</li>
</ul>
</li>
<li>抽象类不能被直接实例化(不能直接通过 new 关键字创建抽象类)，需要通过子类采用向上转型的方式处理<ul>
<li>因为抽象类中可能存在抽象方法，等待子类实现的方法</li>
</ul>
</li>
<li>如果子类继承了抽象父类，必须实现父类的所有抽象方法，否则子类和需要是抽象类</li>
</ul>
<h3 id="接口"><a href="#接口" class="headerlink" title="接口"></a>接口</h3><ul>
<li><p>其实接口就是比抽象类更高级的抽象，当然抽象类也是比类更高级的抽象</p>
</li>
<li><p>接口中只能有方法的定义，而不能有实现</p>
<ul>
<li>类似于一个特殊的抽象类，本身就是一种规范，一种标准，自身没有实现任何功能</li>
</ul>
</li>
<li><p>定义一个接口</p>
<ul>
<li>public interface 接口名{}</li>
</ul>
</li>
<li><p>代码展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 接口被 interface 关键字修饰</span></span><br><span class="line"><span class="comment">// 接口与接口直接只能继承，支持多继承</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">USB</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 接口中不能有实例化的属性</span></span><br><span class="line"><span class="comment">//    public String str;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 接口中不能有构造方法</span></span><br><span class="line"><span class="comment">//    public USB()&#123;&#125;;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 接口中可以有常量属性，创建后必须手动赋值</span></span><br><span class="line">    <span class="comment">// 默认被 public static final 修饰</span></span><br><span class="line">    String VERSION = <span class="string">&quot;3.2&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 接口中的方法默认被 public abstract 修饰</span></span><br><span class="line"><span class="comment">     *  接口中的方法不能有方法体</span></span><br><span class="line"><span class="comment">     *  推荐使用默认的修饰</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">useUSB</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * jdk 1.8 后支持在接口中编写默认方法，可以有方法体</span></span><br><span class="line"><span class="comment">     *  public default 返回值类型 方法名()&#123;...&#125;</span></span><br><span class="line"><span class="comment">     *  将原有的 abstract 修饰符替换为 default 修饰</span></span><br><span class="line"><span class="comment">     *  该方法的出现是为了兼容旧代码，拓展新功能</span></span><br><span class="line"><span class="comment">     *      对修改封闭，对拓展开放</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">default</span> <span class="keyword">void</span> <span class="title">USBUpgrade</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;USB现在支持外界视频功能&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * jdk 1.8 后支持在接口中编写静态方法，可以有方法体</span></span><br><span class="line"><span class="comment">     *  public static 返回值类型 方法名()&#123;...&#125;</span></span><br><span class="line"><span class="comment">     *  将原有的 abstract 修饰符替换为 static 修饰</span></span><br><span class="line"><span class="comment">     *  该方法归接口所有，不存在子类重写</span></span><br><span class="line"><span class="comment">     *  主要作用就是对接口进行相关说明，定义独有的某些方法</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">USBVersion</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;USB的版本为&quot;</span>+VERSION);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="接口小练习"><a href="#接口小练习" class="headerlink" title="接口小练习"></a>接口小练习</h4><ul>
<li>创建一个 LED 接口：brightness属性，turnOn方法，turnOff方法，viewBrightness亮度显示方法</li>
<li>创建一个华硕电脑，继承 Computer 抽象类，实现LED和USB接口，通过实例调用其中的方法</li>
</ul>
<h4 id="接口特点"><a href="#接口特点" class="headerlink" title="接口特点"></a>接口特点</h4><ul>
<li>接口里的所有方法都是抽象方法，默认修饰符为 public abstract ，也是必须的</li>
<li>接口中所有的属性都是常量 public static final </li>
<li>接口不能被实例化(不能new，没有构造方法)</li>
<li>接口支持多实现，通过 implements 实现，接口与接口之间支持多继承</li>
<li>其他类需要实现接口必须实现接口中的所有方法，或者本身是抽象类</li>
<li>jdk1.8 中接口新增默认方法，可以有方法体，解决接口升级问题</li>
<li>jdk1.8 中接口新增静态方法，可以有方法体，对接口进行说明或者定义独有方法</li>
<li>jdk1.9 中接口新增私有方法，可以有方法体，解决接口中代码冗余问题</li>
<li>接口只能被 public 修饰</li>
</ul>
<h3 id="到底是接口还是抽象类？"><a href="#到底是接口还是抽象类？" class="headerlink" title="到底是接口还是抽象类？"></a>到底是接口还是抽象类？</h3><ul>
<li><p>接口和抽象类的区别</p>
<ul>
<li>类可以实现多个接口，类只能单继承抽象类，接口之间可以多继承</li>
<li>接口中不能有构造方法，抽象类中可以</li>
<li>抽象类中可以存在普通属性，接口中属性都是常量</li>
<li>抽象类中可以有普通方法，接口中方法都是抽象的</li>
</ul>
</li>
<li><p>首先区分什么是继承什么是实现</p>
<ul>
<li>继承是 is-a 的关系，dog is a animal，man is a human</li>
<li>实现是 can-do 的关系，dog can flyingCake，man can party</li>
</ul>
</li>
</ul>
<h2 id="面向对象的其他知识"><a href="#面向对象的其他知识" class="headerlink" title="面向对象的其他知识"></a>面向对象的其他知识</h2><h3 id="java-中的值传递"><a href="#java-中的值传递" class="headerlink" title="java 中的值传递"></a>java 中的值传递</h3><ul>
<li><p>什么是值传递？什么是引用传递？</p>
<ul>
<li>值传递：对于变量传递的是一个副本，函数中改变副本不影响原有变量</li>
<li>引用传递：一般是对于变量传递的是实际参数的地址，函数中改变会影响原有变量</li>
</ul>
</li>
<li><p>在 java 中本质上只有值传递，所有的传递都是一次值得拷贝。引用类型拷贝其引用地址值传入方法，基本数据类型拷贝本身。</p>
</li>
<li><p>代码展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Student</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String name;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Student&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;name=&#x27;&quot;</span> + name + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&quot;, age=&quot;</span> + age +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 修改传入的引用类型</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> s</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">change</span><span class="params">(Student s)</span></span>&#123;</span><br><span class="line"><span class="comment">//        s.name = &quot;张三&quot;;</span></span><br><span class="line"><span class="comment">//        s.age = 16;</span></span><br><span class="line">        s = <span class="keyword">new</span> Student();</span><br><span class="line">        s.name = <span class="string">&quot;张三&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 修改传入的基本类型</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> a</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> a)</span></span>&#123;</span><br><span class="line">        a = a + <span class="number">10</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Student stu = <span class="keyword">new</span> Student();</span><br><span class="line">        stu.name = <span class="string">&quot;萧十一郎&quot;</span>;</span><br><span class="line">        stu.age = <span class="number">30</span>;</span><br><span class="line">        <span class="comment">// 对传入的引用类型的值进行修改</span></span><br><span class="line">        stu.change(stu);</span><br><span class="line">        <span class="comment">// 查看原有值是否修改</span></span><br><span class="line">        System.out.println(<span class="string">&quot;stu = &quot;</span> + stu); <span class="comment">// 1，修改了</span></span><br><span class="line">        <span class="keyword">int</span> num = <span class="number">22</span>;</span><br><span class="line">        <span class="comment">// 对传入的基本类型的值进行修改</span></span><br><span class="line">        stu.add(<span class="number">22</span>);</span><br><span class="line">        <span class="comment">// 查看原有值是否修改</span></span><br><span class="line">        System.out.println(<span class="string">&quot;num = &quot;</span> + num); <span class="comment">// 2，没修改</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="java-main-方法"><a href="#java-main-方法" class="headerlink" title="java main 方法"></a>java main 方法</h3><ul>
<li>现在 main 方法中有一个东西大家都会有疑问， args 参数我们一直没用，它到底有什么用呢？</li>
<li>在程序运行时外部传入一些参数，例如一些配置参数等信息</li>
</ul>
<h3 id="java-的静态"><a href="#java-的静态" class="headerlink" title="java 的静态"></a>java 的静态</h3><ul>
<li>static 静态的，可以修饰方法、属性、代码块、类(内部类)<ul>
<li>如果使用 static 修饰属性和方法时，则不再属于实例，而是属于整个类的本身</li>
</ul>
</li>
<li>调用静态属性或者方法时可以使用类名调用(推荐使用)<ul>
<li>类名.静态属性</li>
<li>类名.静态方法</li>
</ul>
</li>
<li>注意：<ul>
<li>通过类名访问静态属性或静态方法，与对象无关，与类有关</li>
<li>静态方法中只能访问静态属性或静态方法</li>
<li>静态方法中不能使用 this 关键字</li>
</ul>
</li>
</ul>
<h4 id="静态代码块"><a href="#静态代码块" class="headerlink" title="静态代码块"></a>静态代码块</h4><ul>
<li><p>代码块，由称之为初始化块，属于类中的成员，由 {} 包裹。代码块没有方法名，没有参数，没有返回值，只有方法体，而且不通过对象或类进行显示调用。它会在类加载或者创建对象时主动调用</p>
</li>
<li><p>代码块可以分为普通代码块和静态代码块</p>
<ul>
<li><p>普通代码块</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// 写在类中，方法之外，每次创建实例都会被调用</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>静态代码块</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">static</span> &#123;</span><br><span class="line">    <span class="comment">// 写在类中，方法之外，在类加载时调用，后续创建实例不再调用</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>所有的成员变量其实都是在构造中进行赋值的</p>
</li>
<li><p>所有的静态变量都是在静态代码块中赋值的</p>
</li>
<li><p>静态方法或代码块中不能使用 this 是因为，this 表示为当前实例。静态执行时实例还没有创建，并且静态中是没有 this 参数的</p>
</li>
</ul>
<h4 id="静态代码块执行顺序"><a href="#静态代码块执行顺序" class="headerlink" title="静态代码块执行顺序"></a>静态代码块执行顺序</h4><ul>
<li><p>代码准备</p>
<ul>
<li><p>Father</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Father</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Father</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;这是父类的构造器&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;这是父类的实例代码块&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;这是父类的静态代码块&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>Son</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Son</span> <span class="keyword">extends</span> <span class="title">Father</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Son</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;这是子类的构造器&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;这是子类的实例代码块&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;这是子类的静态代码块&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>最终的执行顺序</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">这是父类的静态代码块</span><br><span class="line">这是子类的静态代码块</span><br><span class="line">这是父类的实例代码块</span><br><span class="line">这是父类的构造器</span><br><span class="line">这是子类的实例代码块</span><br><span class="line">这是子类的构造器</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<ul>
<li><p>静态总结：</p>
<ul>
<li>静态就是一种特殊的修饰符，被修饰的属性，方法，代码块都会比实例优先加载</li>
<li>静态的内容不属于任何实例，只属于类本身</li>
<li>静态可以极大的节省内存空间，并且访问更加便捷</li>
<li>静态也不是完美的，因为静态不属于实例。所以不存在继承的概念，也就意味着静态不存在多态的概念。并且在使用静态方法时也属于 静态解析。而且静态也操作不了实例变量</li>
</ul>
</li>
</ul>
<h3 id="java-中的-JVM"><a href="#java-中的-JVM" class="headerlink" title="java 中的 JVM"></a>java 中的 JVM</h3><ul>
<li><p>Java Virtual Machine (java虚拟机)的缩写，JVM是一种用于计算设备的 规范，它是一个 虚拟 出来的计算机。JVM包含了类加载器、运行时数据区、执行引擎，JVM可以不需要了解操作系统的具体信息，让java程序在JVM上生成运行的目标代码（字节码），就可以在不同的操作系统上运行JAVA</p>
<ul>
<li>JVM 主流的三大虚拟机</li>
<li>sun/oracle HotSpot：oracle 公司提供的，也是我们现在在学习的</li>
<li>BEA JRokit：orcale 收购，在 JDK 1.8 中被融合到了  HotSpot 中</li>
<li>IBM J9：目前 IBM 主力虚拟机，更加优秀的职责分离与模块化</li>
</ul>
</li>
<li><p>java 的虚拟机宗旨 “一次编译，到处运行” ，也就是虚拟机的跨平台。其实 java 语言与其他的编程语言没有特别大的差别，并不是说 java 语言可以跨平台，而是在不同的平台都有可以使用让 Java 语言运行的环境</p>
</li>
<li><p>虚拟机包括：一套字节码指令集，一组寄存器，一个栈，一个垃圾回收堆，一个存放方法域</p>
</li>
<li><p>PS：虚拟机其实不仅仅只可以编译 java 语言，它还支持除 java 以外的一百多个语言。只要可以编译为 .class 文件的语言它都支持</p>
<p><img src="image/java%E7%9A%84JVM%E5%9B%BE01.png" loading="lazy"></p>
</li>
</ul>
<h4 id="是解释还是编译？"><a href="#是解释还是编译？" class="headerlink" title="是解释还是编译？"></a>是解释还是编译？</h4><ul>
<li><p>解释型：</p>
<ul>
<li>代码没有编译的过程，读一行执行一行，比如 javascript</li>
</ul>
</li>
<li><p>编译型：</p>
<ul>
<li>在运行之前需要将代码先编译成机器指令再运行，比如C语言</li>
</ul>
</li>
<li><p>java会先编译(javac) 但是只能编译为 字节码，字节码文件只能通过 JVM 识别。最后会通过 JVM 进行解释</p>
<p><img src="image/java%E7%9A%84JVM%E5%9B%BE02.png" loading="lazy"></p>
</li>
<li><p>目前而言 JVM 有三个非常重要的编译器：</p>
<ul>
<li>前端编译器，就是 javac</li>
<li>JIT 编译器，将字节码编译为本地机器代码</li>
<li>AOT 编译器，将源代码直接编译为机器码</li>
</ul>
</li>
<li><p>总结：<strong>java 是一种解释型和编译型共存的语言</strong> </p>
</li>
</ul>
<h4 id="动态还是静态？"><a href="#动态还是静态？" class="headerlink" title="动态还是静态？"></a>动态还是静态？</h4><ul>
<li>静态类型语言：<ul>
<li>变量的类型在编译阶段就可以确认</li>
<li>变量的类型在运行时不能修改</li>
<li>例如：java，C。。。</li>
</ul>
</li>
<li>动态类型语言<ul>
<li>变量的类型在运行时确认</li>
<li>变量的类型在运行时可以修改</li>
<li>例如：python，javascript。。。</li>
</ul>
</li>
</ul>
<h4 id="类的加载"><a href="#类的加载" class="headerlink" title="类的加载"></a>类的加载</h4><ul>
<li><p>一个类，经过编译后要形成字节码文件，然后 JVM 需要在第一次主动使用这个类时将这个类加载到方法区</p>
</li>
<li><p>类的整个生命周期包括：加载（Loading）、验证（Verification）、准备 (Preparation)、解析 (Resolution)、初始化 (Initialization)、使用(Using) 和 卸载(Unloading) 7个阶段</p>
<p><img src="image/java%E7%9A%84JVM%E5%9B%BE03.png" loading="lazy"></p>
</li>
<li><p>加载（Loading）</p>
<ul>
<li>将 .class 文件生成一个 Class 类对象</li>
</ul>
</li>
<li><p>验证（Verification）</p>
<ul>
<li>验证 class 文件是否符合 JVM 规范</li>
</ul>
</li>
<li><p>准备 (Preparation)</p>
<ul>
<li>为静态变量分配内存空间并且设置默认值</li>
</ul>
</li>
<li><p>解析 (Resolution)</p>
<ul>
<li>将常量池中的符号引用替换为直接引用(引用地址)</li>
</ul>
</li>
<li><p>初始化 (Initialization)</p>
<ul>
<li>才是真正的执行类操作，调用 <code>&lt;clinit&gt;</code> 方法的过程，生成对象</li>
</ul>
</li>
<li><p>使用(Using)</p>
<ul>
<li>访问对象的方法或者属性</li>
</ul>
</li>
<li><p>卸载(Unloading)</p>
<ul>
<li>一般不主动操作，也不需要操作</li>
</ul>
</li>
</ul>
<h5 id="类加载器"><a href="#类加载器" class="headerlink" title="类加载器"></a>类加载器</h5><ul>
<li><p>类加载器其实就是一段 classloader 代码，通过 全限定类名 来获取这个类的二进制字节流，将字节码文件加载到 方法区。然后在 堆内存中创建一个对应的 Class 对象，对象中就封装了该类的方法区的数据结构</p>
<ul>
<li><p>Bootstrap：启动类加载器，读取 lib 目录中的 jar 包（包名为java，javax，sun开头的）</p>
</li>
<li><p>Extension：扩展类加载器，读取 lib/ext 目录中的 jar 包</p>
</li>
<li><p>AppClassLoader：应用程序类加载器，加载开发者自己编写的类</p>
</li>
<li><p>自定义类加载器：根据需求自己定义，实现 ClassLoader 抽象类</p>
<p><img src="image/java%E7%9A%84JVM%E5%9B%BE04.png" loading="lazy"></p>
</li>
</ul>
</li>
<li><p>类加载的顺序</p>
<ol>
<li><p>在内存中找</p>
</li>
<li><p>通过双亲类加载方式去找</p>
</li>
<li><p>通过自身的方式去加载</p>
<p><img src="image/java%E7%9A%84JVM%E5%9B%BE05.png" loading="lazy"></p>
</li>
</ol>
</li>
<li><p>双亲委派机制保证了Java程序的稳定运行，可以避免类的重复加载，也保证了Java的核心API不被篡改。如果有人想替换系统级别的类：String.java，篡改它的实现。在这种机制下这些系统的类已经被BootstrapclassLoader加载过了，所以并不会再去加载</p>
</li>
<li><p>加载优先级：启动类、扩展类、应用类加载器</p>
</li>
</ul>
<h4 id="垃圾回收算法"><a href="#垃圾回收算法" class="headerlink" title="垃圾回收算法"></a>垃圾回收算法</h4><ul>
<li><p>标记算法</p>
<ul>
<li>引用计数算法</li>
<li>可达性分析算法</li>
</ul>
</li>
<li><p>垃圾回收算法</p>
<ul>
<li>标记清除算法</li>
<li>复制算法</li>
<li>标记整理算法</li>
<li>分代收集算法</li>
</ul>
</li>
<li><p>垃圾收集器</p>
<p><img src="image/java%E7%9A%84JVM%E5%9B%BE06.png" loading="lazy"></p>
</li>
<li><p>垃圾收集器总结</p>
</li>
</ul>
<p><img src="image/java%E7%9A%84JVM%E5%9B%BE07.png" loading="lazy"></p>
</div><div id="reward-container"><span class="hty-icon-button button-glow" id="reward-button" title="Donate" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === &quot;none&quot;) ? &quot;block&quot; : &quot;none&quot;;"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-hand-coin-line"></use></svg></span><div id="reward-comment">I want you.</div><div id="qr" style="display:none;"><div style="display:inline-block"></div><div style="display:inline-block"></div><div style="display:inline-block"></div></div></div><ul class="post-copyright"><li class="post-copyright-author"><strong>Post author: </strong>zzl</li><li class="post-copyright-link"><strong>Post link: </strong><a href="http://zilongzeng.gitee.io/web/2022/10/07/java%E4%B8%AD%E7%BA%A7/" title="java中级">http://zilongzeng.gitee.io/web/2022/10/07/java%E4%B8%AD%E7%BA%A7/</a></li><li class="post-copyright-license"><strong>Copyright Notice: </strong>All articles in this blog are licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank" rel="noopener" title="CC BY-NC-SA 4.0 "><svg class="icon"><use xlink:href="#icon-creative-commons-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-by-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-nc-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-sa-line"></use></svg></a> unless otherwise stated.</li></ul></section></article><div class="post-nav"><div class="post-nav-item"></div><div class="post-nav-item"><a class="post-nav-next" href="/web/2022/09/13/java%E5%88%9D%E7%BA%A7/" rel="next" title="java初级"><span class="post-nav-text">java初级</span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-right-s-line"></use></svg></a></div></div></div><div class="hty-card" id="comment"><div class="comment-tooltip text-center"><span>要不要和我说些什么？</span><br></div></div></main><footer class="sidebar-translate" id="footer"><div class="copyright"><span>&copy; 2019 – 2022 </span><span class="with-love" id="animate"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-cloud-line"></use></svg></span><span class="author"> zzl</span></div><div class="powered"><span>Powered by <a href="https://hexo.io" target="_blank" rel="noopener">Hexo</a> v5.4.0</span><span class="footer-separator">|</span><span>Theme - <a rel="noopener" href="https://github.com/YunYouJun/hexo-theme-yun" target="_blank"><span>Yun</span></a> v1.6.2</span></div></footer><a class="hty-icon-button" id="back-to-top" aria-label="back-to-top" href="#"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-up-s-line"></use></svg><svg class="progress-circle-container" viewBox="0 0 100 100"><circle class="progress-circle" id="progressCircle" cx="50" cy="50" r="48" fill="none" stroke="#0078E7" stroke-width="2" stroke-linecap="round"></circle></svg></a></div></body></html>